首页
学习
活动
专区
圈层
工具
发布

cURL代码0
EN

Stack Overflow用户
提问于 2014-05-06 21:26:28
回答 2查看 2K关注 0票数 0

我看过以前关于cURL和HTTP code 0的帖子,但它们没有帮助。

我可以用下面的脚本从localhost调用www.bambooping.com -也就是说,test_curl.php on localhost调用test_curl2.php on bambooping.com。但是,如果我在bambooping.com上运行它,我会得到HTTP 0。(我知道在同一主机上调用它是愚蠢的-它只是为了隔离问题。)

bambooping.com上,没有设置safe_mode,并且已经编译了curl (也就是说,应该是因为我可以cURL in)。这非常奇怪--调用主机正在阻止cURL。为什么用cURL调用会像这样失败,而用cURL调用同一台主机却没问题?

test_curl.php:

代码语言:javascript
复制
<?php
error_reporting(E_ALL); ini_set("display_errors", 1);
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

// OK cool - then let's create a new cURL resource handle
$ch = curl_init();

// Now set some options (most are optional)

// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);

// Set a referer
//    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);

// make it blank - then it is ignored - otherwise, checked and error returned!
curl_setopt($ch, CURLOPT_REFERER, '');

// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);

// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

//    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

// Download the given URL, and return output
$output = curl_exec($ch);

print_r(curl_getinfo($ch));

// Close the cURL resource, and free system resources
curl_close($ch);

return $output;
}

$str = curl_download("http://www.bambooping.com/test_curl2.php");
echo $str;
?>

test_curl2.php

代码语言:javascript
复制
<?php
echo "I am here";
?>

curl_getinfo为:

代码语言:javascript
复制
Array
(
[url] => http://www.bambooping.com/test_curl2.php
[content_type] => 
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 4.3E-5
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0
[redirect_time] => 0
)

想法?我刚喝完..。谢谢-

EN

回答 2

Stack Overflow用户

发布于 2014-05-06 21:54:30

请检查是否有curl错误

它告诉你问题所在

代码语言:javascript
复制
<?php
    if(curl_errno($ch))  echo 'Curl error: ' . curl_error($ch);  
?>
票数 1
EN

Stack Overflow用户

发布于 2014-05-06 21:52:59

www.bambooping.com的服务器可能位于阻止传出HTTP请求的防火墙后面。即使是同一台服务器,请求仍然需要在野外解析DNS。

您可以编辑服务器上的hosts文件,使其包含127.0.0.1 www.bampooing.com。或者,您可以将URL更改为http://127.0.0.1/test_curl2.php,因为此本地主机域可能未被防火墙阻止。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23496043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档