/** * curl 請求發送 * @param $url * @param $header * @param $content * @return mixed */ function send($url, $header, $content){ $ch = curl_init(); if(substr($url,0,5)=='https'){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 從證書中檢查SSL加密算法是否存在 } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content)); $response = curl_exec($ch); if($error=curl_error($ch)){ die($error); } curl_close($ch); return $response; } $url = 'http://www.example.com'; $header = array('token:JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU','language:zh','region:GZ'); $content = array( 'name' => 'fdipzone' ); $response = send($url, $header, $content); $data = json_decode($response, true); echo 'POST data:'; echo '<pre>'; print_r($data['post']); echo '</pre>'; echo 'Header data:'; echo '<pre>'; print_r($data['header']); echo '</pre>';
/** * 獲取全部 以 HTTP開頭的header參數 * @return array */ function getAllHeaders(){ $headers = array(); foreach($_SERVER as $key=>$value){ if(substr($key, 0, 5)==='HTTP_'){ $key = substr($key, 5); $key = str_replace('_', ' ', $key); $key = str_replace(' ', '-', $key); $key = strtolower($key); $headers[$key] = $value; } } return $headers; } $post_data = $_POST; //獲取post參數做爲對比 $header = getAllHeaders(); $ret = array(); $ret['post'] = $post_data; $ret['header'] = $header; //echo json_encode($_SERVER,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT); //注意:$_SERVER 能夠獲取全部 header的參數 //全部在header中自定義的參數 例如:自定義參數名:username 那麼 獲取方法 $_SERVER['HTTP_USERNAME'] 全部均是大寫 //echo $_SERVER['HTTP_USERNAME']; header('content-type:application/json;charset=utf8'); echo json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
用戶的相關信息能夠放在header中 作網站 身份驗證等操做
歡迎你們,收藏轉發哦!php