当我运行curl时,我得到状态0

时间:2011-09-19 05:38:20

标签: php curl

我正在使用此代码从互联网获取页面...但我得到结果状态0

$url='http://www.jiwlp.com';

$this->url = $url;

if (isset($this->url)) {

    // start cURL instance 
    $this->ch = curl_init ();

    // this tells cUrl to return the data
    curl_setopt ($this->ch, CURLOPT_RETURNTRANSFER, 1);

    // set the url to download 
    curl_setopt ($this->ch, CURLOPT_URL, $this->url); 

    // follow redirects if any 
    curl_setopt($this->ch,CURLOPT_FOLLOWLOCATION, true); 

    // tell cURL if the data is binary data or not 
    curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, $this->binary); 

    $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";

    curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false); 
    curl_setopt($this->ch, CURLOPT_VERBOSE, 1);  
    curl_setopt($this->ch, CURLOPT_USERAGENT, $useragent); 
    curl_setopt($this->ch, CURLOPT_TIMEOUT, 5);

    // grabs the webpage from the internet 
    $this->html = curl_exec($this->ch);
    $this->status = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);

    print_r(curl_getinfo($this->ch)); // closes the connection
    curl_close ($this->ch);
}

任何人都可以解释我做错了什么。

由于

4 个答案:

答案 0 :(得分:0)

响应状态已存储在$this->status中,我假设您指的是HTTP响应状态代码,因此不是

// grabs the webpage from the internet 
$this->html = curl_exec($this->ch);  
$this->status = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);

print_r(curl_getinfo($this->ch));

尝试打印出$this->status

// grabs the webpage from the internet 
$this->html = curl_exec($this->ch);  
$this->status = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);

print_r($this->status);

答案 1 :(得分:0)

此版本适用于我,删除了oo

$url = 'http://www.jiwlp.com';

if(isset($url)){
$ch = curl_init();
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch,CURLOPT_BINARYTRANSFER,$binary);

    $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
 rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_VERBOSE,1);
    curl_setopt($ch,CURLOPT_USERAGENT,$useragent);
    curl_setopt($ch,CURLOPT_TIMEOUT,5);
    curl_exec($ch);
    $status = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    print_r(curl_getinfo($ch));
    curl_close($ch);
}

答案 2 :(得分:0)

我使用此功能获取http站点/链接状态:

<?php
function get_link_status($url, $timeout = 10) 
{
$ch = curl_init();
// set cURL options
$opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser
            CURLOPT_URL => $url,            // set URL
            CURLOPT_NOBODY => true,         // do a HEAD request only
            CURLOPT_TIMEOUT => $timeout);   // set timeout
curl_setopt_array($ch, $opts);
curl_exec($ch); // do it!
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); // find HTTP status
curl_close($ch); // close handle
echo $status; //or return $status;
    //example of check
    if ($status == '301') { echo 'This is redirected';}
}

get_link_status('http://site.com');
?>

答案 3 :(得分:0)

检查php禁用功能中的CURL。

如果在网络服务器中禁用CURL_EXEC,php将不会给出错误,而是[http_code] =&gt; 0 header_size] =&gt; 0 .......

从php页面运行phpinfo()是获取已禁用的php函数列表的一种方法。