zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    Springboot操作域对象
    Thymeleaf的条件判断和迭代遍历
    Thymeleaf的字符串与变量输出的操作
    window 2003 实现多用户远程登录
    非常漂亮js动态球型云标签特效代码
    Echarts运用
    sass、less和stylus的安装使用和入门实践
    java,js 解unicode
    Java上传下载excel、解析Excel、生成Excel
    小知识点应用
  • 原文地址:https://www.cnblogs.com/sener/p/13935838.html
Copyright © 2011-2022 走看看