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

发布于 2024-12-05 09:32:43 字数 1299 浏览 0 评论 0原文

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

我做错了什么?

I am using this code to get page from internet, but I get result status 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);
}

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请止步禁区 2024-12-12 09:32:43

这个版本对我有用,删除了 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);
}

this version works for me, removed the 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);
}
好多鱼好多余 2024-12-12 09:32:43

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

The response status is already stored in $this->status, I'm assuming you are referring to HTTP response status codes, so instead of

// 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));

Try printing out $this->status instead.

// 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);
月下伊人醉 2024-12-12 09:32:43

检查 php 禁用功能中的 CURL。

如果 Web 服务器中禁用了 CURL_EXEC,则 php 不会给出错误,而是会 [http_code] => 0 header_size] => 0 .......

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

check CURL in php disabled function.

if CURL_EXEC is disabled in the webserver, php wont give error instead will [http_code] => 0 header_size] => 0 .......

run phpinfo() from a php page is one of a way to get the list of disabled php functions.

李白 2024-12-12 09:32:43

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

I use this function to get http site/link status:

<?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');
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文