如何讓php通過header發送自訂資料

來源:互聯網
上載者:User
本文將介紹如何通過header發送自訂資料。發送請求時,除了可以使用$_GET/$_POST發送資料,也可以把資料放在header中傳輸過去。

發送header:

我們定義了三個參數,token、language、region,放入header發送過去

<?php$url = 'http://www.example.com';$header = array('token:JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU','language:zh','region:GZ');$content = array(        'name' => 'fdipzone');$response = tocurl($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>';/** * 發送資料 * @param String $url     請求的地址 * @param Array  $header  自訂的header資料 * @param Array  $content POST的資料 * @return String */function tocurl($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;}?>

接收header

我們可以在$_SERVER中擷取header資料,自訂的資料都是使用HTTP_作為首碼的,所以可以把HTTP_首碼的資料讀出。

<?php$post_data = $_POST;$header = get_all_headers();$ret = array();$ret['post'] = $post_data;$ret['header'] = $header;header('content-type:application/json;charset=utf8');echo json_encode($ret, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);/** * 擷取自訂的header資料 */function get_all_headers(){    // 忽略擷取的header資料    $ignore = array('host','accept','content-length','content-type');    $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);            if(!in_array($key, $ignore)){                $headers[$key] = $value;            }        }    }    return $headers;}?>

輸出:

POST data:Array(    [name] => fdipzone)Header data:Array(    [token] => JxRaZezavm3HXM3d9pWnYiqqQC1SJbsU    [language] => zh    [region] => GZ)

本文講解了如何讓php通過header發送自訂資料 ,更多相關內容請關注php中文網。

相關推薦:

如何通過mysql 利用inet_aton和inet_ntoa來處理ip地址資料

php json資料使用gzip壓縮輸出的相關內容

如何通過php來使用http_build_query,parse_url,parse_str建立與解析url

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.