public function actionCurl(){ $data['DATA']='{"NAME":"c","LEGEND":"c;c","GENDER":"","CITY":0,"BIRTHDAY":"","COMPANY":"","JOB":"","WEBSITE":"","ADDRESS":"","NOTE":"","CONTACTS":[],"IMS":[],"FLAGS":[]}'; $headers=array(); $headers[]='X-Requested-With:XMLHttpRequest'; $headers[]='Cookie:token=3cedf24b188fd7ac57176256a2a14879-b419e6ebb414d661a6e86a30cd633ce5; PHPSESSID=ddfrjb4p8evu3cumgnsveet2o3; _ga=GA1.2.1014541392.1437467254; _gat=1'; $headers[]='Referer:http://www.crm.com/newContact.php'; $headers[]='Content-Type:application/x-www-form-urlencoded; charset=UTF-8'; $headers[]='Content-Length:315'; $headers[]='Host:www.crm.com'; $headers[]='DNT:1'; $headers[]='Pragma:no-cache'; $headers[]='Connection:Keep-Alive'; $headers[]='User-Agent:Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'; $headers[]='Accept:application/json, text/javascript, */*; q=0.01'; $headers[]='Accept-Encoding:gzip, deflate'; $headers[]='Accept-Language:zh-CN'; for ($i = 0; $i < 10; $i++) { $res = $this->curls($data, 'http://www.crm.com/handler/handleAddContact.php ', 'POST', $headers); print_r($res); } } /** * curl请求 * @author lid * @param array $data 要请求的array数组 * @param str $url 请求的地址 * @param str $method 大写 POST GET * @param array $headers 扩展包头信息 * @return string */ public static function curls($data, $url, $method = 'POST', $headers = array()) { $ch = curl_init(); $method_upper = strtoupper($method); if ($method_upper == 'POST') { curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } else { $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($data) ? http_build_query($data) : $data); curl_setopt($ch, CURLOPT_URL, $url); } curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method_upper); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 20); if ($headers) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { exit(curl_error($ch)); } curl_close($ch); return $tmpInfo; }
CURLOPT_POSTFIELDS的这个设置中,亦可以直接发送数组的,但是这样做的后果是严重增加的请求的时间,耗时大约在2秒多,同样的操作用socket方式操作则正常,在毫秒级别。
数组:curl_setopt($ch, CURLOPT_POSTFIELDS, $arrData);
修改为:curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));http_build_query — 生成 URL-encode 之后的请求字符串。
curl 发送XML数据
$xml = ('');$url = "https://wx.api.zf.com/wxnotify.php ";$header[] = "Content-type: text/xml";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HTTPHEADER, $header);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$res = curl_exec($ch);curl_close($ch); Tove Jani Reminder Don\'t forget me this weekend!