【问题标题】:File_get_contents($url): failed to open streamFile_get_contents($url):打开流失败
【发布时间】:2014-08-18 16:38:28
【问题描述】:

我有一个脚本,我使用以下方法读取文件:

file_get_contents(urlencode($url));

我收到此错误:

failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request

我尝试了this,但仍然收到错误消息。 我试过这个:

ini_set('default_socket_timeout', 120);

这个:

$opts = array('http'=>array('timeout' => 120));
         $context = stream_context_create($opts);
         $resul = file_get_contents($url,0,$context);

还有这个:

$opts = array('http'=>array('timeout' => 120,'header'=>'Connection : close'));
         $context = stream_context_create($opts);
         $resul = file_get_contents($url,false,$context);

你能帮我弄清楚为什么会出现错误吗?

【问题讨论】:

  • urlencode 应在将参数附加到 URL 时调用,而不是在整个 URL 上调用。
  • 可能您对流摆弄所做的任何事情都无济于事,因为您使用的网址本身就是错误的。例如语法错误、错误参数等......你不能盲目地对整个 url 进行 urlencode,因为这也会对必要的东西进行编码。例如http://example.com 变为 http%3A%2F%2Fexample.com,现在它不再是正确的 url。
  • urlencode 正在编码方案和主机 (http%3A%2F%2Fexample%2Ecom),返回错误的 URL。正如@Barman 所说,只需对参数进行编码,而不是对整个 URL 进行编码。另外,请使用rawurlencode
  • @Barmar 刚刚编辑我只为参数做了 urlencode 但仍然收到错误,在某些情况下我什至不使用 urlencode 并且我有同样的错误...@Ben 是什么rawurlencode ??
  • @MarcB 谢谢我只是想得到解释......需要一些时间

标签: php


【解决方案1】:

您只需要对“querystring”进行编码,提取查询并对其进行编码,在将编码后的查询附加到您的“url”之后。

注意:file_get_contents 在“php.ini”中需要allow_url_fopen=On,尝试使用curl

示例(在代码中阅读我的 cmets)

注意:此示例会出现连接错误和 http 错误

<?php
//Set your page example
$uri = 'http://localhost/path/webservice.php?callback=&id=153&provenance=153&ret=a:1:{s:5:"infos";a:8:{s:8:"civilite";s:3:"Mme";s:5:"lname";s:0:"";s:5:"fname";s:8:"Nathalie";s:5:"email";s:17:"tometnata@free.fr";s:3:"tel";s:0:"";s:7:"adresse";s:0:"";s:6:"date_n";s:14:"10:"01/06/1969";s:2:"cp";s:0:"";}}';

//extract url
$parsed_url = parse_url($uri);

//Create fixed url
$fixed_url = $parsed_url['scheme'] . '://' . $parsed_url['host'] . $parsed_url['path'];

//If exists query
if (isset($parsed_url['query'])) {
    $output = array();
    $result = array();

    //Extract querystring
    parse_str($parsed_url['query'], $output);

    //Encode values in querystring
    forEach($output as $k => $v) {
        $result[] = $k . '=' . rawurlencode($v);
    }

    //Append encoded querystring
    $fixed_url .= '?' . implode('&', $result);
}

echo 'GET url: ', $fixed_url, '<br>';

//Get result in page
$ch = curl_init();
$timeout = 30; //set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $fixed_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

$errornum = curl_errno($ch);

$info = curl_getinfo($ch);

$status = (int) $info['http_code'];

if ($errornum !== 0) {
    echo 'Error: ', curl_error($ch);
    $file_contents = NULL;
} else if ($status !== 200) {
    echo 'http_error: ', $status;
    $file_contents = NULL;
} else {
    echo 'Result:<hr>';
    echo $file_contents;
}

curl_close($ch);
?>

启用卷曲

【讨论】:

  • 感谢和抱歉迟到的反馈,当我使用 curl_init() 时出现错误!我想这是一个我应该安装的插件......我说得对吗?
  • 一直在使用你得到我的函数,现在我面临一个奇怪的问题,即当我的 url 中有数组时;功能停止工作......这是一个解决方案......谢谢
猜你喜欢
  • 2010-10-16
  • 2017-09-17
  • 2011-02-18
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多