request 获取请求参数时字段尾部的空格或者回车获取不到?
我请求时参数携带了回车,获取到的参数最后结尾部分的回车不见了?var_dump打印出来长度也少了4位,是什么引起的?
这是我的测试代码:
$params['app_key'] = APP_KEY;
$params['access_token'] = APP_TOKEN;
$params['timestamp'] = time();
$params['test'] = "ceshi ";
$params['sig'] = getSig($params);
var_dump($params);
echo $rs = request('http://dev.apiserver.com/v1/Orders/importO...');
生成sig
function getSig($params,$debug=true){
if(empty($params) || !is_array($params)) return;
ksort($params);
$signStr = http_build_query($params);
$signStr = APP_SECRET.'?'.$signStr.APP_SECRET;
//if($debug) echo "待加密的字符串:".$signStr."
";
return MD5($signStr);
}
http请求
function request($url, $params=null, $method='GET',$timeout= 30){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
//https 请求
if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if($method == "GET"){
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPGET, true);
}
if($method == "POST"){
curl_setopt($ch, CURLOPT_POST, true);
if(is_array($params) && 0 < count($params)){
$postBodyString = "";
$postMultipart = true;
foreach($params as $k => $v){
if("@" != substr($v, 0, 1)){
$postBodyString .= "$k=" . urlencode($v) . "&";
}else{
$postMultipart = true;
}
}
unset($k, $v);
if ($postMultipart){
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}else{
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
}
}
}
$reponse = curl_exec($ch);
curl_close($ch);
return $reponse;
}
下面是我var_dump 的结果
array(5) {
["app_key"]=>
string(32) "cd25ee6ca398668acd3549ba5f8e66fd"
["access_token"]=>
string(32) "ad44a9f8f68867e042ac8f11af24c1f5"
["timestamp"]=>
int(1539656253)
["test"]=>
string(10) "ceshi "
["sig"]=>
string(32) "b3abe160fdaae10848ca816b4a4657bf"
}
array(5) {
["app_key"]=>
string(32) "cd25ee6ca398668acd3549ba5f8e66fd"
["access_token"]=>
string(32) "ad44a9f8f68867e042ac8f11af24c1f5"
["timestamp"]=>
string(10) "1539656253"
["test"]=>
string(5) "ceshi"
["sig"]=>
string(32) "b3abe160fdaae10848ca816b4a4657bf"
}
test这个字段长度不相等?
下面是我后台的执行代码
class BaseController extends \App\Http\Controllers\Controller
{
protected $store;
protected $api_client;
public function __construct(Request $request)
{
var_dump($request->all());exit;
$this->initOauth($request);
if(!$this->checkSign($request)) {
$params = $request->all();
unset($params['sig']);
throw new V1Exception('sig 无效,签名错误', 40005);
}
}
....
推荐文章: