php使用file_get_content封装转发请求
2018-12-22
/**
* * 请求接口数据
* @param return 返回请求的数据
*/
public function requestData($apiUrl, $receiveData = [])
{
if (empty($apiUrl)) {
$this->ajaxReturn('url is required');
return false;
}
$successMsg = '请求数据成功';
$errorMsg = '请求.net接口返回数据为空';
// http_build_query 转义成查询字符串,如果是转发请求会产生问题,
// 需要urldecode解码
$postData = urldecode(http_build_query($receiveData));
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postData
]
];
$context = stream_context_create($options);
$result = file_get_contents($apiUrl, false, $context);
if ($result) {
$data = [
'code' => 200,
'data' => $result,
'msg' => $successMsg
];
} else {
$data = [
'code' => 404,
'data' => $result,
'msg' => $errorMsg
];
}
return $this->ajaxReturn($data);
}
其中有两点坑
http_build_query生成 URL-encode 之后的请求字符串- 这里发生了转码,转发到请求接口可能会报错
- 表单提交的的字符传进行了
escape转码,转发请求参数前需要进行htmlspecialcahrs_decode解码,防止转发的是转码后的字符串,会导致请求数据或提交数据报错
下一步,封装 cUrl 进行请求
遇到问题不去解决,很难受,查看了一下php官方文档手册,总结用curl封装请求
public function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
简单例子
<?php
// 创建一个新 cURL 资源
$ch = curl_init();
// 设置 URL 和相应的选项
$options = array(CURLOPT_URL => 'http://www.example.com/',
CURLOPT_HEADER => false
);
curl_setopt_array($ch, $options);
// 抓取 URL 并把它传递给浏览器
curl_exec($ch);
// 关闭 cURL 资源,并且释放系统资源
curl_close($ch);
?>
比较不错的封装
<?php
function httpRquest($url,$method=0,$curl_data )
{
if(strtolower($method) == 'post'){
$method = 1
}
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POST => $method, // i am sending post data
CURLOPT_POSTFIELDS => $curl_data, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1 // 输出所有的信息,写入到STDERR,
);
$ch = curl_init($url);
curl_setopt_array($ch,$options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch) ;
$header = curl_getinfo($ch);
curl_close($ch);
// $header['errno'] = $err;
// $header['errmsg'] = $errmsg;
// $header['content'] = $content;
return $header;
}
$curl_data = "var1=60&var2=test";
$url = "https://www.example.com";
$response = get_web_page($url,$curl_data);
print '<pre>';
print_r($response);
?>
本文介绍如何使用PHP的file_get_contents及cURL库封装HTTP请求,包括GET和POST方法,解决了在转发请求中出现的URL编码问题,提供了详细的代码示例。
3556

被折叠的 条评论
为什么被折叠?



