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

3b6akqbq  于 2022-11-13  发布在  其他
关注(0)|答案(4)|浏览(108)

我正在使用此代码从互联网获取页面,但我得到的结果状态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);
}

我做错了什么?

but5z9lq

but5z9lq1#

这个版本为我工作,删除了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);
}
kcugc4gi

kcugc4gi2#

响应状态已经存储在$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);
kuuvgm7e

kuuvgm7e3#

在php禁用函数中检查CURL。
如果CURL_EXEC在网络服务器中被禁用,php将不会给予错误,而是将[http_code] =〉0 header_size] =〉0 ....
从一个php页面运行phpinfo()是一种获取被禁用php函数列表的方法。

t5fffqht

t5fffqht4#

我使用这个函数来获取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://example.com');
?>

相关问题