【问题标题】:No Result trying to get echo from other file with cUrl没有结果尝试使用 cUrl 从其他文件中获取回显
【发布时间】:2015-11-14 13:20:04
【问题描述】:

我无法弄清楚为什么我没有从我的设置中得到任何回报。我正在使用 cUrl 从另一个 php 文件中获取返回的结果(事件)。

在我的第一个文件中,我有一个这样的案例:

...
case 'list':    
    $events = $_crs->listCourse($_REQUEST["from"], $_REQUEST["to"], $_REQUEST["category"], $_REQUEST["limit"]);
    return $events;
break;
...

在执行文件中我有另一个这样的功能:

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

而这个函数就是被这行代码调用的:

$list = url_get_contents($_SERVER['DOCUMENT_ROOT'] . "CalendarEvents.php?set=list&from=$from&to=$to&category=&limit=15");

【问题讨论】:

    标签: php arrays curl


    【解决方案1】:

    与大多数 PHP 函数一样,cURL 函数返回有意义的值。你可能想测试 $output 的值,如果它是 FALSE,可视化 cURL 错误信息。

    <?php // /demo/curl_get_example.php
    /**
     * Demonstrate the basics of cURL GET-method request
     * Something like demo/curl_get_example.php?url=http://twitter.com
     *
     * http://curl.haxx.se/libcurl/c/libcurl-errors.html
     */
    error_reporting(E_ALL);
    
    Class GET_Response_Object
    {
        public $href, $title, $http_code, $errno, $info, $document;
    
        public function __construct($href, $get_array=[], $title=NULL)
        {
            // ACTIVATE THIS TO AVOID TIMEOUT FOR LONG RUNNING SCRIPT
            // set_time_limit(10);
    
            // STORE THE CALL INFORMATION
            $this->href  = $href;
            $this->title = $title;
    
            // PREPARE THE GET STRING
            $get_string = http_build_query($get_array);
            if ($get_string) $get_string = '?' . $get_string;
    
            // MAKE THE REQUEST
            if (!$this->my_curl($href, $get_string))
            {
                // ACTIVATE THIS TO SEE THE ERRORS AS THEY OCCUR
                // trigger_error("Errno: $this->errno; HTTP: $this->http_code; URL: $this->href", E_USER_WARNING);
            }
        }
    
        protected function my_curl($url, $get_string, $timeout=3)
        {
            // PREPARE THE CURL CALL
            $curl = curl_init();
    
            // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
            $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            $header[] = "Cache-Control: max-age=0";
            $header[] = "Connection: keep-alive";
            $header[] = "Keep-Alive: 300";
            $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
            $header[] = "Accept-Language: en-us,en;q=0.5";
            $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK
    
            // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
            curl_setopt( $curl, CURLOPT_URL,            $url . $get_string  );
            curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0'  );
            curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
            curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
            curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
            curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
            curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
            curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
            curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );
            curl_setopt( $curl, CURLOPT_VERBOSE,        TRUE   );
            curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE   );
    
            // SET THE LOCATION OF THE COOKIE JAR (WILL BE OVERWRITTEN)
            curl_setopt( $curl, CURLOPT_COOKIEFILE,     'cookie.txt' );
            curl_setopt( $curl, CURLOPT_COOKIEJAR,      'cookie.txt' );
    
            // IF USING SSL, THIS INFORMATION MAY BE IMPORTANT
            // http://php.net/manual/en/function.curl-setopt.php#110457
            // http://php.net/manual/en/function.curl-setopt.php#115993
            // http://php.net/manual/en/function.curl-setopt.php#113754
            // REDACTED IN 2015 curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
            curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
            curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );
    
            // RUN THE CURL REQUEST AND GET THE RESULTS
            $this->document  = curl_exec($curl);
            $this->errno     = curl_errno($curl);
            $this->info      = curl_getinfo($curl);
            $this->http_code = $this->info['http_code'];
            curl_close($curl);
    
            // RETURN DOCUMENT SUCCESS SIGNAL
            return $this->document;
        }
    }
    
    
    // USAGE EXAMPLE: YOU COULD HAVE SOMETHING LIKE THIS
    $url = isset($_GET["url"]) ? $_GET["url"] : 'http://twitter.com';
    
    // BUT BECAUSE IT IS ON MY SERVER, I HAVE HARD-CODED THIS
    $url = 'https://twitter.com/RayPaseur';
    
    // TRY THE REMOTE WEB SERVICE
    $response = new Get_Response_Object($url);
    
    // SHOW THE WORK PRODUCT
    echo "<pre>";
    if (!$response->document) var_dump($response);
    echo htmlentities($response->document);
    
    // SHOW THE COOKIES, IF ANY
    echo PHP_EOL;
    echo file_get_contents('cookie.txt');
    

    【讨论】:

    • 对不起,我什至不知道从哪里开始?你能否让你的分析器尽可能简单?并根据我的问题中的代码给我一个例子?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 2020-09-29
    相关资源
    最近更新 更多