開發中經常遇到接口請求這個功能,後臺也不例外,由於遇到了,因此寫一篇。html
前段時間作商城後臺時,須要用到第三方物流接口查詢物流信息。api
/**** * @param $url * @param $post_data * @return bool|string * 第三方物流接口請求 */ function send_post($url,$post_data) { $postdata = http_build_query($post_data); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超時時間(單位:s) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } $order_number="訂單號"; $post_data = array( 'company_id' => '開放平臺ID', 'msg_type' => 'TRACEINTERFACE_NEW_TRACES', 'data' => "[\"$order_number\"]", 'data_digest' => '簽名' ); $data =send_post("http://japi.zto.cn/gateway.do",$post_data); echo $data;
瀏覽器打印數據:瀏覽器
$url='http://www.baidu.com/'; $html = file_get_contents($url); echo $html;
$searchUrl = 'URL?content='; if(!empty($_GET['content'])) { $searchUrl .= $_GET['content']; } echo file_get_contents($searchUrl);
本文簡單介紹了發送 HTTP 請求,項目中使用到的,就提寫出來了,也方便本身之後查看。app