php 常用get post http請求


  1 /**
  2      * http-GET
  3      * 
  4      */
  5     $url = "https://www.baidu.com";
  6     $params = [
  7         "uid"   =>  $_REQUEST["uid"],
  8         "token" =>  $_REQUEST["token"],
  9     ];
 10     $result = file_get_contents($url);
 11 
 12     function curl_file_get_contents($url){
 13         $ch = curl_init();
 14         curl_setopt($ch, CURLOPT_URL, $url);
 15         curl_setopt($ch, CURLOPT_TIMEOUT, 5);
 16         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 17         $res = curl_exec($ch);
 18         curl_close($ch);
 19         return $res;
 20     }
 21     $result = curl_file_get_contents($url);
 22 
 23     /**
 24      * http-POST
 25      * 
 26      */
 27     function http_post($url, $data) {
 28         $postdata = http_build_query($data);
 29         $options = [
 30             'http' => [
 31                 'method' => 'POST',
 32                 'header' => 'Content-type:application/x-www-form-urlencoded',
 33                 'content' => $postdata,
 34                 'timeout' => 15 * 60
 35             ]
 36         ];
 37         $context = stream_context_create($options);
 38         $result = file_get_contents($url, false, $context);
 39         return $result;
 40     }
 41 
 42     function http_post2($url, $data) {
 43         $ch = curl_init();
 44         curl_setopt($ch, CURLOPT_POST, 1);
 45         curl_setopt($ch, CURLOPT_URL, $url);
 46         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 47         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 48         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
 49             'Content-Type: application/json; charset=utf-8',  
 50             'Content-Length:' . strlen($data))
 51         );
 52         $return_content = curl_exec($ch);
 53         curl_close($ch);
 54         return $return_content;
 55     }
 56 
 57     
 58     $head = [];
 59     $head[] = 'Timestamp:'.'1600000000';
 60     $head[] = 'Nonce:'.'Nonce';
 61     $head[] = 'Signature:'.'Signature';
 62     $head[] = 'Content-Type:text/plain;charset=utf-8';
 63     // $head[] = 'Content-Type: application/json';
 64     /**自定義header頭信息 */
 65     function http_post3($url, $data, $header)
 66     {
 67         $curl = curl_init();
 68         curl_setopt($curl, CURLOPT_URL, $url);
 69         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 70         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查
 71         curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬用戶使用的瀏覽器
 72         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉
 73         curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設置Referer
 74         curl_setopt($curl, CURLOPT_POST, 1); // 發送一個常規的Post請求
 75         curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設置超時限制防止死循環
 76         curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區域內容
 77         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 78         curl_setopt($curl, CURLOPT_HTTPHEADER, $header);//設置自定義header頭信息
 79         $res = curl_exec($curl); // 執行操作
 80         if (curl_errno($curl)) {
 81             echo 'Errno'.curl_error($curl);//捕抓異常
 82         }
 83         curl_close($curl); // 關閉CURL會話
 84         return $res; // 返回數據,json格式
 85     }
 86 
 87     function http_post4($url, $data, $header){
 88         $postdata = http_build_query($data);
 89         $opts = array('http' =>
 90             array(
 91                 'method'  => 'POST',
 92                 'header'  => $header,
 93                 'content' => $postdata,
 94                 'timeout' => 30
 95             )
 96         );
 97         $context = stream_context_create($opts);
 98         $result = file_get_contents($url, false, $context);
 99         return $result;
100     }
101 
102     function http_post5($url, $data, $headers){
103         $ch = curl_init();
104         curl_setopt($ch,CURLOPT_URL,$url);
105         if(!empty($headers)){
106             curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
107         }
108         curl_setopt($ch, CURLOPT_POST, 1);
109         /**http_build_query 吧請求的數據轉換成 k=v&k2=v2 發送請求*/
110         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
111         curl_setopt($ch, CURLOPT_TIMEOUT, 5);
112         curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
113         curl_setopt($ch, CURLOPT_HEADER,0);//如果想把一個頭包含在輸出中,設置這個選項為一個非零值。
114         curl_setopt($ch, CURLINFO_HEADER_OUT,1);
115         $json = curl_exec($ch);
116         curl_close($ch);
117         return $json;
118     }
 1 /**其他*****/
 2     /**
 3      * 獲取url
 4      */
 5     function get_app_url(){
 6         $scheme = $_SERVER['REQUEST_SCHEME']; //協議
 7         $domain = $_SERVER['HTTP_HOST']; //域名/主機
 8         $requestUri = $_SERVER['REQUEST_URI']; //請求參數
 9         $getUrl = $scheme . "://" . $domain . $requestUri;
10         return $getUrl;
11     }
12 
13     /**
14      * 跳轉指定url
15      * 
16      */
17     header("Location:https://www.baidu.com");
18 
19     /**
20      * 是否支持https判斷
21      */
22     function is_https()
23     {
24         if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
25         {
26             return TRUE;
27         }
28         elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
29         {
30             return TRUE;
31         }
32         elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
33         {
34             return TRUE;
35         }
36         return FALSE;
37     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM