本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

PHP curl_exec() 无法在线工作,但在本地主机上工作正常,因为 CURLOPT_POSTFIELDS 未将数据附加到 http 发布请求

发布于2021-11-13 14:41     阅读(1065)     评论(0)     点赞(19)     收藏(2)


我无法弄清楚为什么此代码在我的 PC(本地主机)上本地运行,但在 google app-engine 上不在线?会不会是 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

那里似乎没有设置 postfields。我被这些困住了。任何帮助将不胜感激。


解决方案


这可能是由于两个原因:

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


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.phpheidong.com/blog/article/198495/31c0f7c6bb943937a063/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

19 0
收藏该文
已收藏

评论内容:(最多支持255个字符)