php file_get_contents 报错

昨天在工作中遇到这样一个问题:

PHP Warning:  file_get_contents(http://platform.handsmart.mobi/userplatform/pay/face.php?a=getMoneyAndMsgNum&amp;appid=1&amp;username=3c5122d46b38451731189397098d115b&amp;uid=bda30b0c713e44da6c8e4feb7e78a290&amp;secret=181fa343509968304d51a09e170a4801) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: Connection refused in /usr/local/rakoo/userplatform/item/action/fastBuyItemAction.php on line 21

开始一看报错,发现url跟自己写的好像是不一样,所有的&都转义成了&amp;,就固执的定位为你程序编码错误,是系统把url给转义了,所以也就一直在网上找&转义这个问题的解决方案。但最终发现不是url有问题,在url中出现&amp;是正常的,上面那个错误的意思是:file_get_contents无法获取到那个url中的数据流,打开流失败,所以正在的问题应该是那请求响应超时了,所以php报错了。为什么超时呢?这就可能是网络不好了,响应时间超出了php等待时间。要解决这个问题,可以把file_get_contents的超时时间设置的长一些,具体做法如下:

 

原文地址:PHP stream_cont     原文作者:banu

作用:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

函数原型:resource stream_context_create ([ array $options [, array $params ]] )

用法

例子一:

<?php
$opts = array(   'http-->array(
'method'=&gt;"GET",
'header'=&gt;"Accept-language: en/r/n" .
"Cookie: foo=bar/r/n"
)
);

$context = stream_context_create($opts);

/* Sends an http request to www.heliximitate.cn
with additional headers shown above */
$fp = fopen('http://www.heliximitate.cn', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

例子二:

<?php
  $opts = array(    'http-->array(
'method'=&gt;"GET",
'header'=&gt;"Accept-language: en/r/n" .
"Cookie: foo=bar/r/n"
)
);

$context = stream_context_create($opts);
?>

You would setup the header this way:

<?php
$opts = array(    'http-->array(
'method'=&gt;"GET",
'header'=&gt;array("Accept-language: en",
"Cookie: foo=bar",
"Custom-Header: value")
)
);

$context = stream_context_create($opts);
?>

例子三:

<?php
$opts = array('http' => array('proxy' => 'tcp://127.0.0.1:8080', 'request_fulluri' => true));
$context = stream_context_create($opts);

$data = file_get_contents('http://www.heliximitate.cn', false, $context);

echo $data;

?>

例子四:

<?php
function do_post_request($url, $postdata, $files = null)
{
    $data = "";
    $boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);

    //Collect Postdata
    foreach($postdata as $key => $val)
    {
        $data .= "--$boundary/n";
        $data .= "Content-Disposition: form-data; name=/"".$key."/"/n/n".$val."/n";
    }

    $data .= "--$boundary/n";

    //Collect Filedata
    foreach($files as $key => $file)
    {
        $fileContents = file_get_contents($file['tmp_name']);

        $data .= "Content-Disposition: form-data; name=/"{$key}/"; filename=/"{$file['name']}/"/n";
        $data .= "Content-Type: image/jpeg/n";
        $data .= "Content-Transfer-Encoding: binary/n/n";
        $data .= $fileContents."/n";
        $data .= "--$boundary--/n";
    }

    $params = array('http' => array(
           'method' => 'POST',
           'header' => 'Content-Type: multipart/form-data; boundary='.$boundary,
           'content' => $data
        ));

   $ctx = stream_context_create($params);
   $fp = fopen($url, 'rb', false, $ctx);

   if (!$fp) {
      throw new Exception("Problem with $url, $php_errormsg");
   }

   $response = @stream_get_contents($fp);
   if ($response === false) {
      throw new Exception("Problem reading data from $url, $php_errormsg");
   }
   return $response;
}

//set data (in this example from post)

//sample data
$postdata = array(
    'name' => $_POST['name'],
    'age' => $_POST['age'],
    'sex' => $_POST['sex']
);

//sample image
$files['image'] = $_FILES['image'];

do_post_request("http://www.heliximitate.cn", $postdata, $files);
?>

 

反思:看到报错,别轻率的定位错误,要认真的看懂错误的根本原因,然后下手。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值