Php 你的车坏了吗?

Php 你的车坏了吗?,php,curl,Php,Curl,我正在使用以下代码: $agent= 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0'; $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_URL, "www.example.com"); curl_setopt($ch, CURLOPT_RET

我正在使用以下代码:

$agent= 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0';

$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, $agent);

curl_setopt($ch, CURLOPT_URL, "www.example.com");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

$output = curl_exec($ch);

echo $output;
但它会重定向到这样的位置:

http://localhost/aide.do?sht=_aide_cookies_
而不是指向URL页面

有人能帮我解决问题吗

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
说:

CURLOPT_FOLLOWLOCATION

如果在服务器作为HTTP头的一部分发送的任何“Location:”头之后加上TRUE(注意,这是递归的,PHP将在发送的相同数量的“Location:”头之后加上,除非设置了CURLOPT_MAXREDIRS) 如果只有URL重定向,请参阅以下代码,我已经为您编写了文档,以便您可以轻松直接地使用它,您有两个主要的cURL选项控制URL重定向(CURLOPT_FOLLOWLOCATION/CURLOPT_MAXREDIRS):

上面的代码处理URL重定向问题,但它不处理Cookie(您的本地主机URL似乎正在处理Cookie)。如果您希望处理来自cURL资源的cookies,那么您可能需要查看以下cURL选项: 曲奇饼 卷曲文件 库基耶尔酒店

有关更多详细信息,请访问以下链接:

如果将代码缩进四个空格,可读性会更好。+1我喜欢用一次搜索就能找到我想要的东西
// create a new cURL resource
$ch = curl_init();

// The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");

// The contents of the "User-Agent: " header to be used in a HTTP request.
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0");

// TRUE to include the header in the output.
curl_setopt($ch, CURLOPT_HEADER, false);

// TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

// The maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT_FOLLOWLOCATION.
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

// grab URL and pass it to the output variable
$output = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

// Print the output from our variable to the browser
print_r($output);