我有一个脚本,我使用以下方法读取文件:
file_get_contents(urlencode($url));
我收到此错误:
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request
I tried this http://php.net/manual/fr/function.file-get-contents.php,但我仍然收到错误。
我试过这个:
ini_set('default_socket_timeout', 120);
This:
$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);
你能帮我找出为什么我会收到错误吗?
您只需要编码“querystring”,提取查询并对其进行编码,在附加编码后的查询“url”后。
Note: file_get_contents
需要allow_url_fopen=On
在“php.ini”中,尝试使用curl
示例(阅读我在代码中的注释)
注意:此示例出现连接错误和 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:"[email protected] /cdn-cgi/l/email-protection";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);
?>
启用卷曲
- Windows(Xampp):https://stackoverflow.com/a/1347340/1518921 https://stackoverflow.com/a/1347340/1518921
- Linux(如 debian):https://stackoverflow.com/a/11724633/1518921 https://stackoverflow.com/a/11724633/1518921
- Mac OSX(可能已经过时):https://stackoverflow.com/a/11354731/1518921 https://stackoverflow.com/a/11354731/1518921
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)