自己 做个记录,你们看不懂拉倒, 不重要
1.请求 需要签名的接口很正常的请求,没有特殊设置
/** * 发送http post 请求,发送到api接口,已经写好了apikey * * @param string $type 类型,array、json类型 * @param $data 提交参数,类型需要和 type 一致 * @param $url 提交地址 * @return mixed|string */ function _sendPostRequest($url, $content=null,$headers = null) { $content = is_null($content)?array():$content; $ch = curl_init(); if (substr($url, 0, 5)=='https') { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // 用户加密信息 if (!empty($headers)){ $head_key = array('Authorization:'.$headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $head_key); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $tmpInfo; }
2.
$jsonData = json_encode($data); // 2. 请求服务器处理 $url = "地址"; list ($returnCode, $returnContent) = doPost($url, $jsonData);
/** * 该方法可以公用 //pingan */ function doPost($url, $jsonData) { $cid = ; // 需要换成您自己的 企业id $appId = ""; // 需要换成您自己的 appId $appSecret = ""; // 需要换成您自己的 appSecret return http_post_json_crm($url, $jsonData, $cid, $appId, $appSecret); }
/** * post 业务请求 * 该方法可以公用 * @param string $url * @param string $jsonData * @param int $cid * @param string $appId * @param string $appSecret */ function http_post_json_crm($url, $jsonData, $cid, $appId, $appSecret) { // 1. 获取当前时间戳 $timeStamp = time() * 1000; // 2. 获取签名 $sign = getSign($timeStamp, $appId, $appSecret); // 3. 封装请求头 $head = array( 'Content-Type: application/json; charset=utf-8', 'X-Ec-Cid: ' . $cid, 'X-Ec-Sign: ' . $sign, 'X-Ec-TimeStamp: ' . $timeStamp, 'Content-Length: ' . strlen($jsonData) ); // 3. 传入http 参数 $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // https 支持 - 对认证证书来源的检查 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 请求数据放这里 curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); // head curl_setopt($ch, CURLOPT_HTTPHEADER, $head); // 请求服务器 $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // 组织,返回结果和响应码 return array( $httpCode, $response ); }
3.。。。
/** * curl post数据 * @param $url * @param $post_data * @return mixed */ public function _postCurl($url, $content=null,$headers = null){ $content = is_null($content)?array():$content; $ch = curl_init(); if (substr($url, 0, 5)=='https') { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // 用户加密信息 if (!empty($headers)){ $head_key =array( "Accept: application/json", "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", ); curl_setopt($ch, CURLOPT_HTTPHEADER, $head_key); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt ($ch, CURLOPT_ENCODING ,'gzip'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $tmpInfo; }
4.。header需要设置
$headers = 'Token token='.$user_token.', device=open_api, version_code=9.9.9';
$res = self::_sendPostRequest('https://lxcrm.weiwenjia.com/api/v2/customers',$list,$headers);
/** * 发送http post 请求,发送到api接口,已经写好了apikey * * @param string $type 类型,array、json类型 * @param $data 提交参数,类型需要和 type 一致 * @param $url 提交地址 * @return mixed|string */ function _sendPostRequest($url, $content=null,$headers = null) { $content = is_null($content)?array():$content; $ch = curl_init(); if (substr($url, 0, 5)=='https') { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // 用户加密信息 if (!empty($headers)){ $head_key = array('Authorization:'.$headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $head_key); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $tmpInfo; }
乱糟糟的各种 格式 ,