为什么这个 php cURL 函数不能工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4955180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why this php cURL function can't work
提问by runeveryday
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
i find this function from the internet. when i test it in a php file using this code $returned_content = get_data('http://google.com');
but it can't work.and get a "301 Moved Permanently" The document has moved here. error. why?
我从互联网上找到了这个功能。当我使用此代码在 php 文件中对其进行测试时,$returned_content = get_data('http://google.com');
它无法工作。并得到“301 永久移动”文档已移至此处。错误。为什么?
回答by Pekka
According to your comments, you are getting a 302 status code. Try
根据您的评论,您收到了 302 状态代码。尝试
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
to follow 30x redirects.
遵循 30x 重定向。
Manual on curl_setopt()
手动开启 curl_setopt()
回答by Shrinath
add one more option to your get_data function :
为您的 get_data 函数再添加一个选项:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
Google is redirecting you to the local google servers and your curl call currently isn't chasing redirects.
Google 正在将您重定向到本地 google 服务器,并且您的 curl 调用当前没有跟踪重定向。
oh yeah,
and do a var_dump($returned_content);
to see the results :P
哦,是的,
然后var_dump($returned_content);
查看结果:P
回答by bandam
Might be just as easy as adding an "echo" Try following line :
可能就像添加“回声”一样简单尝试以下行:
echo get_data('http://google.com');
Also, please check if cURL module is activate in the PHP.ini
另外,请检查是否在 PHP.ini 中激活了 cURL 模块
To find out easily just put following lines in a new file called info.php
要轻松查找,只需将以下几行放在名为 info.php 的新文件中
<?php
phpinfo();
?>
Search for cUrl.
搜索卷曲。
If you can't find it please have a look at http://php.net/manual/en/book.curl.phpto see what is involved to install the php curl module
如果找不到,请查看http://php.net/manual/en/book.curl.php以了解安装 php curl 模块所涉及的内容
OR add following to your curl options:
或将以下内容添加到您的卷曲选项中:
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);
And sorry for the double posts.
对不起,双重帖子。