api - PHP file_get_contents()返回“无法打开流:HTTP请求失败!”
我在从PHP代码调用url时遇到问题。 我需要使用PHP代码中的查询字符串来调用服务。 如果我在浏览器中键入url,它可以正常工作,但如果我使用file-get-contents()来进行调用,我会得到:
警告:file-get-contents([http:// ....])无法打开流:HTTP请求失败! HTTP / 1.1 202接受于......
我使用的代码是:
$query=file_get_contents('http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv');
echo($query);
就像我说 - 从浏览器调用,它工作正常。 有什么建议么?
我还尝试过另一个网址,例如:
$query=file_get_contents('http://www.youtube.com/watch?v=XiFrfeJ8dKM');
这工作正常......可能是我需要调用的网址中有第二个http://吗?
9个解决方案
97 votes
尝试使用cURL。
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
?>
James Hall answered 2019-09-20T17:10:35Z
25 votes
这可能是你的问题吗?
注意:如果要打开包含特殊字符的URI(例如空格),则需要使用urlencode()对URI进行编码。
SilentGhost answered 2019-09-20T17:11:07Z
21 votes
$lurl=get_fcontent("http://ip2.cc/?api=cname&ip=84.228.229.81");
echo"cid:".$lurl[0]."
";
function get_fcontent( $url, $javascript_loop = 0, $timeout = 5 ) {
$url = str_replace( "&", "&", urldecode(trim($url)) );
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
if ($response['http_code'] == 301 || $response['http_code'] == 302) {
ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
if ( $headers = get_headers($response['url']) ) {
foreach( $headers as $value ) {
if ( substr( strtolower($value), 0, 9 ) == "location:" )
return get_url( trim( substr( $value, 9, strlen($value) ) ) );
}
}
}
if ( ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) && $javascript_loop < 5) {
return get_url( $value[1], $javascript_loop+1 );
} else {
return array( $content, $response );
}
}
?>
pangeli answered 2019-09-20T17:11:26Z
18 votes
allow_url_fopen使用fopen()包装器,因此限制通过php.ini中的allow_url_fopen选项访问URL。
您将需要更改您的php.ini以启用此选项或使用替代方法,即cURL - 迄今为止最流行的,并且诚实地,标准方式来完成您要执行的操作。
Michael Wales answered 2019-09-20T17:12:01Z
7 votes
我注意到你的URL中有空格。 我认为这通常是一件坏事。 尝试使用编码URL
$my_url = urlencode("my url");
然后打电话
file_get_contents($my_url);
看看你有没有更好的运气
shady answered 2019-09-20T17:12:45Z
6 votes
您基本上需要通过请求发送一些信息。
尝试这个,
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
//Basically adding headers to the request
$context = stream_context_create($opts);
$html = file_get_contents($url,false,$context);
$html = htmlspecialchars($html);
这对我有用
d_bhatnagar answered 2019-09-20T17:13:30Z
3 votes
我不确定参数(mpaction,format),如果为amazonaws页面或##。##指定它们。
尝试urlencode()url。
alexn answered 2019-09-20T17:14:04Z
3 votes
我遇到了类似的问题,我解析了youtube网址。 代码是;
$json_is = "http://gdata.youtube.com/feeds/api/videos?q=".$this->video_url."&max-results=1&alt=json";
$video_info = json_decode ( file_get_contents ( $json_is ), true );
$video_title = is_array ( $video_info ) ? $video_info ['feed'] ['entry'] [0] ['title'] ['$t'] : '';
然后我意识到$this->video_url包含空格。 我用trim($this->video_url)解决了这个问题。
也许它会帮助你。 祝好运
Emre Karataşoğlu answered 2019-09-20T17:14:47Z
-4 votes
用这个
file_get_contents($my_url,null,null);
Amit Chawla answered 2019-09-20T17:15:10Z