PHP curl_exec()无法在线工作但在localhost上工作正常,因为CURLOPT_POSTFIELDS未将数据附加到http post请求

时间:2015-08-04 13:12:01

标签: php google-app-engine curl dns

我无法弄清楚为什么这段代码在我的电脑(localhost)上本地工作但在谷歌应用引擎上没有在线工作?它可以是PHP版本的问题吗?

<?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;



}

它没有给出任何输出。我也试图从curl_error()得到错误,但它没有返回任何东西。我试图提出解决方案,但找不到任何东西。请帮助。

通过启用curlopt_verbose我得到了:

* 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

似乎没有设置后场。我陷入了这些困境。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

可能是由于两个原因:

  1. 您从未在实时服务器上安装php-curl。
  2. 在该服务器上限制的特定网址。