PHP curl_exec () does not work on the net but works fine on localhost as CURLOPT_POSTFIELDS does not bind data to the HTTP post

I can't figure out why this code works locally on my PC (localhost) but not online on Google App Engine? Could this be a PHP version issue?

<?php

include_once 'simplehtmldom/simple_html_dom.php';


function curl_exec_follow($ch, &$maxredirect = null) {


curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
$mr = $maxredirect === null ? 5 : intval($maxredirect);



    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

    if ($mr > 0)
    {
        $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        $newurl = $original_url;

        $rch = curl_copy_handle($ch);

        curl_setopt($rch, CURLOPT_HEADER, true);
        curl_setopt($rch, CURLOPT_NOBODY, true);
        curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
        do
        {
            curl_setopt($rch, CURLOPT_URL, $newurl);

            $header = curl_exec($rch);
            if (curl_errno($rch)) {
                $code = 0;
            } else {
                $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                if ($code == 301 || $code == 302) {
                    preg_match('/Location:(.*?)\n/i', $header, $matches);
                    $newurl = trim(array_pop($matches));

                    // if no scheme is present then the new url is a
                    // relative path and thus needs some extra care
                    if(!preg_match("/^https?:/i", $newurl)){
                        $newurl = $original_url . $newurl;
                    }
                } else {
                    $code = 0;
                }
            }
        } while ($code && --$mr);

        curl_close($rch);

        if (!$mr)
        {
            if ($maxredirect === null)
                trigger_error('Too many redirects.', E_USER_WARNING);
            else
                $maxredirect = 0;

            return false;
        }
        curl_setopt($ch, CURLOPT_URL, $newurl);
    }
  return curl_exec($ch);
}
    function getContentofEgov( $egovid, $birthdate, $examtype, $currentsem) {
    $batchyear="";

    $cookie_file_path = "cookie.txt";   
    if(file_exists($cookie_file_path))
        unlink($cookie_file_path);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt ($ch, CURLOPT_POST, TRUE);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, "LoginForm[username]=".$egovid."&LoginForm[password]=".$birthdate."");
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt ($ch, CURLOPT_HEADER, 0);
    //curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);    // provides warning on gae
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_file_path);

    curl_setopt($ch, CURLOPT_URL,'http://egov.ddit.ac.in/index.php?r=site/login');  
    $output1 = curl_exec_follow($ch);

    echo $output1;

    if(!empty($output1)){
    $scraped_data = str_get_html($output1);
        if(!empty($scraped_data)){
    $table =$scraped_data->find('table',1) ;
            if(!empty($table)){
    foreach($table->find('tr') as $key=>$value){
        if((trim(strip_tags($value->find('th',0)->plaintext)))== "Batch Year"){
            //echo trim(strip_tags($value->find('td',0)));
            $batchyear= trim(strip_tags($value->find('td',0)->plaintext));
            echo $batchyear;
        }

        if((trim(strip_tags($value->find('th',0)->plaintext)))== "Old Student Code"){

            $username= substr(trim(strip_tags($value->find('td',0)->plaintext)),-5);
        }

    }
            }}}
    else{

        return "Internal Server Error.";
    }

    curl_setopt($ch, CURLOPT_URL,'http://egov.ddit.ac.in/index.php?r=tblstudentmst/academicHistory');
    $output2 = curl_exec_follow($ch);
    echo $output2;
    curl_setopt ($ch, CURLOPT_POST, TRUE);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, "sessionno=".$currentsem."&batchyear=".$batchyear."");
    curl_setopt($ch, CURLOPT_URL,'http://egov.ddit.ac.in/index.php?r=tblstudentmst/'.$examtype.'Relational');
    $output3 = curl_exec_follow($ch);
    echo $output3;
    if(file_exists($cookie_file_path))
        unlink($cookie_file_path);


    return $output3;



}

      

He gives no conclusion. I also tried to get error from curl_error () but it returns nothing. I tried looking for a solution but couldn't find anything. help.

Turning on curlopt_verbose I got:

* timeout on name lookup is not supported
*   Trying 203.88.131.36...
* Connected to egov.ddit.ac.in (203.88.131.36) port 80 (#0)
> POST /index.php?r=site/login HTTP/1.1
Host: egov.ddit.ac.in
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)            Gecko/20041107 Firefox/1.0
Accept: */*
Content-Length: 61
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 61 out of 61 bytes
< HTTP/1.1 302 Found
< Date: Wed, 05 Aug 2015 19:52:36 GMT
< Server: Apache/2.2.3 (CentOS)
< X-Powered-By: PHP/5.3.3
* Added cookie PHPSESSID="r5ndtpkqje5tq1cgmr85pcop90" for domain egov.ddit.ac.in, path /, expire 0 
< Set-Cookie: PHPSESSID=r5ndtpkqje5tq1cgmr85pcop90; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
* Replaced cookie PHPSESSID="9n2ng1cufjg5sc5trcisoartn6" for domain egov.ddit.ac.in, path /, expire 0
< Set-Cookie: PHPSESSID=9n2ng1cufjg5sc5trcisoartn6; path=/
< Location: http://egov.ddit.ac.in/index.php
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
< 
* Connection #0 to host egov.ddit.ac.in left intact

      

It seems that the postfields are not set. I am stuck in them. Any help would be appreciated.

+3


source to share


1 answer


Perhaps this is due to two reasons:



  • you have never installed php-curl on a live server.
  • a specific url restricted on this server.
0


source







All Articles