php通過header發送自訂資料

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

發送header:

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

$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'
';print_r($data['post']);echo'
';echo'Header data:';echo'
';print_r($data['header']);echo'
';/** * 發送資料 * @param String $url 請求的地址 * @param Array $header 自訂的header資料 * @param Array $content POST的資料 * @return String */functiontocurl($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_首碼的資料讀出。

$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資料 */functionget_all_headers(){// 忽略擷取的header資料$ignore = array('host','accept','content-length','content-type');    $headers = array();    foreach($_SERVERas$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)

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
  • ').text(i)); }; $numbering.fadeIn(1700); }); });

    以上就介紹了php通過header發送自訂資料,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.