nice2
file_get_contents 错误的请求
nice2 发表于 9-3 11:28 1分钟前, 0回/0阅 ( 编辑|删除 )
讨论区 » 技术问答
分享到
新浪微博腾讯微博
收藏+0
举报
踩 顶 0我用 php 的 file_get_conten 获取地址返回信息,出现[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request这个错误信息,allow_url_fopen = On 这个也是开启的,我试着用 curl 发送请求结果 直接是 400 bad request现在不知道怎么解决了
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
// 模拟获取内容函数
function vget($url, $ip, $user_agent)
{
$curl = curl_init (); // 启动一个CURL会话
//if (IS_PROXY) {
curl_setopt($curl, CURLOPT_PROXY, $ip); //代理服务器地址,格式:8.8.8.8:8080
//}
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
@curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求
//curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']); // 读取上面所储存的Cookie信息,如果有登录验证的才要用到
curl_setopt($curl, CURLOPT_TIMEOUT, 1200); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec ($curl); // 执行操作 if (curl_errno($curl))
{
echo 'Errno' . curl_error($curl);
} curl_close ($curl); // 关闭CURL会话
return $tmpInfo; // 返回数据
}试一试吧! 不行再说
$data = array(
'client_id' => $_SESSION['sina_token'],
'client_secret' => $_SESSION['sina_token_secret'],
'grant_type' => 'authorization_code',
'redirect_uri' => $_SESSION['callback'],
'code' => $_REQUEST['code'],
);
$postdata = http_build_query($data);
$postdata = str_replace(" ", '%20', $postdata);
$opts = array(
'http' => array(
'method'=>"POST",
'header'=>"POST /oauth2/access_token HTTP/1.0\r\n User-Agent: Sae T OAuth2 v0.1\r\n Host: api.weibo.com\r\n Accept: */*\r\n Accept-Encoding: deflate, gzip\r\n API-RemoteIP: ".$_SERVER["REMOTE_ADDR"]."\r\n Content-Length: ".strlen($postdata)."\r\n Content-Type: application/x-www-form-urlencoded",
'timeout'=>30,
)
); $context = stream_context_create($opts);
$result = @file_get_contents($token_api.'?'.$postdata,false,$context);
那是其他平台接口,不能泄露的,问题已经解决了.
url 中多了一个空格,但是很难发现