标签:close hone nbsp color doc remove one rem 测试
请求测试接口,如下:
$data = [‘mobilephone‘=>‘123‘,‘password‘=>‘124‘]; $result = curlrequest($apiUrl,$data); if($result[‘statusCode‘] == 200){ echo json_encode($result); }else{ echo json_encode($result); }
curl请求url方法,如下:
/** * @param string $url 请求地址 * @param string $data 请求数据 * @param string $method 请求方式 * @return array 一维数组 */ function curlrequest($url,$data = ‘‘,$method = ‘POST‘){ $ch = curl_init(); //初始化CURL句柄 curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而s不是直接输出 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式 curl_setopt($ch,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: $method"));//设置HTTP头信息 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串 $document = curl_exec($ch);//执行预定义的CURL $statusCode = curl_getinfo($ch,CURLINFO_HTTP_CODE); //获取HTTP请求状态码~ curl_close($ch); $document = json_decode(removeBOM($document),true); $document[‘statusCode‘] = $statusCode; return $document; }
如果返回的json结果中出现乱码,那么需要去除BOM,如下:
/** * @param string $str 字符串 * @return string 去除BOM以后的字符串 */ function removeBOM($str = ‘‘) { if (substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) { $str = substr($str, 3); } return $str; }
标签:close hone nbsp color doc remove one rem 测试
原文地址:http://www.cnblogs.com/jingxiaoniu/p/7994198.html