php file_get_contents 报错
昨天在工作中遇到这样一个问题:
PHP Warning: file_get_contents(http://platform.handsmart.mobi/userplatform/pay/face.php?a=getMoneyAndMsgNum&appid=1&username=3c5122d46b38451731189397098d115b&uid=bda30b0c713e44da6c8e4feb7e78a290&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跟自己写的好像是不一样,所有的&都转义成了&,就固执的定位为你程序编码错误,是系统把url给转义了,所以也就一直在网上找&转义这个问题的解决方案。但最终发现不是url有问题,在url中出现&是正常的,上面那个错误的意思是: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"=>"GET",
"header"=>"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"=>"GET",
"header"=>"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"=>"GET",
"header"=>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);
?>
反思:看到报错,别轻率的定位错误,要认真的看懂错误的根本原因,然后下手。