PHP file_get_contents() 返回“无法打开流:HTTP 请求失败!"; [英] PHP file_get_contents() returns "failed to open stream: HTTP request failed!"

查看:62
本文介绍了PHP file_get_contents() 返回“无法打开流:HTTP 请求失败!";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 PHP 代码调用 url 时遇到问题.我需要使用 PHP 代码中的查询字符串调用服务.如果我在浏览器中输入 url,它工作正常,但如果我使用 file-get-contents() 进行调用,我得到:

I am having problems calling a url from PHP code. I need to call a service using a query string from my PHP code. If I type the url into a browser, it works ok, but if I use file-get-contents() to make the call, I get:

警告:file-get-contents(http://....) 无法打开流:HTTP 请求失败!HTTP/1.1 202 接受...

Warning: file-get-contents(http://.... ) failed to open stream: HTTP request failed! HTTP/1.1 202 Accepted in ...

我使用的代码是:

$query=file_get_contents('http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv');
echo($query);

就像我说的 - 从浏览器调用,它工作正常.有什么建议吗?

Like I said - call from the browser and it works fine. Any suggestions?

我也尝试过使用另一个网址,例如:

I have also tried with another url such as:

$query=file_get_contents('http://www.youtube.com/watch?v=XiFrfeJ8dKM');

这很好用……难道我需要调用的 url 中有第二个 http:// 吗?

This works fine... could it be that the url I need to call has a second http:// in it?

推荐答案

尝试使用 cURL.

<?php

$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);

?>

这篇关于PHP file_get_contents() 返回“无法打开流:HTTP 请求失败!";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆