PHP file_get_contents() 返回 "failed to open stream: HTTP request failed!"

标签 php api query-string file-get-contents

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

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

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

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

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

这很好用...可能是我需要调用的网址中有第二个 http:// 吗?

最佳答案

尝试使用 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() 返回 "failed to open stream: HTTP request failed!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/697472/

相关文章:

php - 使用 id 作为引用的数组连接

java - 第一次在 Java 中创建 API

javascript - 如何使用onchange事件?

php - CURLOPT_PUT 与 CURLOPT_POSTFIELDS

android - 如何正确跟踪播放位置? [Android 媒体广播通知 Spotify API]

php - 在 PHP 的 require_once 中使用查询字符串

javascript - Request.QueryString 不接收值

javascript - 用于在 Google 表格中分解查询参数的 Google 应用脚本

php - JavaScript奇怪的错误:“意外的')'”

php - 使用搜索/分页将大型 MYSQL 结果显示到表中的最佳方法是什么?