[英]using curl from localhost
I'm just trying to fetch the yahoo web page.我只是想获取雅虎 web 页面。 www.yahoo.com
www.yahoo.com
If I run my simple script from my hosted site, it works.如果我从我的托管站点运行我的简单脚本,它就可以工作。
If I try it from my localhost.如果我从本地主机尝试。 All I get is a header response with: "w32.fp.re1.yahoo.com uncompressed/chunked Wed Apr 27 15:13:48 PDT 2011"
我得到的只是 header 响应:“w32.fp.re1.yahoo.com 未压缩/分块 Wed Apr 27 15:13:48 PDT 2011”
Here is my code:这是我的代码:
<?php
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, "http://www.example.org/yay.htm");
// 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);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
print curl_download('http://www.yahoo.com/');
?>
Actually, the result starts with实际上,结果开始于
HTTP/1.1 302 Found
which means there's a Location
header in there.这意味着那里有一个
Location
header。 And there is:还有:
Location: http://nl.yahoo.com/?p=us
This is just the response body:这只是响应正文:
<!-- w20.fp.ird.yahoo.com uncompressed/chunked Wed Apr 27 17:08:09 PDT 2011 -->
You need to tell cURL to follow Location headers.您需要告诉 cURL 遵循 Location 标头。 That's it.
而已。
The name of the option is CURLOPT_FOLLOWLOCATION
.选项的名称是
CURLOPT_FOLLOWLOCATION
。 Set it to true:将其设置为真:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
PS附言
After following 1
Location header, this is the start of the response body when I run your code + FOLLOWLOCATION:在关注
1
位置 header 之后,这是我运行您的代码 + FOLLOWLOCATION 时响应正文的开始:
<!DOCTYPE html>
<html lang="nl-NL" class="y-fp-bg y-fp-pg-grad bkt732">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.