• PHP请求第三方接口的函数


     1 <?php
     2     
     3     public function HttpGet($url){
     4         $curl = curl_init ();
     5         curl_setopt ( $curl, CURLOPT_URL, $url );
     6         curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
     7         // curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
     8         // curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36');
     9         
    10         //如果用的协议是https则打开鞋面这个注释
    11         //curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
    12         // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    13         
    14         $res = curl_exec ( $curl );
    15         curl_close ( $curl );
    16         return $res;
    17     }
    18 
    19      public function HttpPost($url,$param){
    20 
    21         $ch = curl_init();
    22         //如果$param是数组的话直接用
    23         curl_setopt($ch, CURLOPT_URL, $url);
    24         //如果$param是json格式的数据,则打开下面这个注释
    25         // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    26         //         'Content-Type: application/json',
    27         //         'Content-Length: ' . strlen($param))
    28         // );
    29         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    30         curl_setopt($ch, CURLOPT_POST, 1);
    31         curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
    32         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    33 
    34         //如果用的协议是https则打开鞋面这个注释
    35         // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    36         // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    37 
    38         $data = curl_exec($ch);
    39 
    40         curl_close($ch);
    41         return $data;
    42 
    43     }
    44     
    45 ?>
  • 相关阅读:
    2020.5.28.第十三周java上机练习
    2020.5.22第十二周java作业
    2020.5.21.第十二周java上机练习
    2020.5.15.java第十一周作业
    2020.5.14.第十一周上机练习
    leetcode02大数相加
    leetcode算法题01
    近期wxss总结
    近期Freecodecamp问题总结
    freecodecamp数字转化成罗马数字
  • 原文地址:https://www.cnblogs.com/JeromeZ/p/6043276.html
Copyright © 2020-2023  润新知