为什么这个php cURL功能无法正常工作

run*_*day 8 php

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;
}
Run Code Online (Sandbox Code Playgroud)

我从互联网上找到这个功能.当我使用此代码在php文件中测试它$returned_content = get_data('http://google.com');但它无法工作.并获得"301 Moved Permanently"文档已移至此处.错误.为什么?

Pek*_*ica 32

根据您的评论,您将获得302状态代码.尝试

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
Run Code Online (Sandbox Code Playgroud)

遵循30倍重定向.

手册 curl_setopt()

  • @runeveryday因为远程服务器传递了一个`302`头重定向,并在正文中给出了一个`location:`字符串.普通浏览器将自动跟随新位置(触发新请求).有了卷曲,你必须明确告诉它遵循.请参阅此页面,了解状态代码及其含义:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.htmlhttp://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html (2认同)