zoukankan      html  css  js  c++  java
  • 两个php函数

    以前忽略的函数。今天看t.qq.com开发接口,发现了。

    1、http_build_query($params);

    2、parse_str($r, $out);

    curl用于向服务器发送http(s)请求。

     1 /**
     2  * HTTP请求类
     3  * @author xiaopengzhu <xp_zhu@qq.com>
     4  * @version 2.0 2012-04-20
     5  */
     6 class Http
     7 {
     8     /**
     9      * 发起一个HTTP/HTTPS的请求
    10      * @param $url 接口的URL 
    11      * @param $params 接口参数   array('content'=>'test', 'format'=>'json');
    12      * @param $method 请求类型    GET|POST
    13      * @param $multi 图片信息
    14      * @param $extheaders 扩展的包头信息
    15      * @return string
    16      */
    17     public static function request( $url , $params = array(), $method = 'GET' , $multi = false, $extheaders = array())
    18     {
    19         if(!function_exists('curl_init')) exit('Need to open the curl extension');
    20         $method = strtoupper($method);
    21         $ci = curl_init();
    22         curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
    23         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 3);
    24         curl_setopt($ci, CURLOPT_TIMEOUT, 3);
    25         curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    26         curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
    27         curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
    28         curl_setopt($ci, CURLOPT_HEADER, false);
    29         $headers = (array)$extheaders;
    30         switch ($method)
    31         {
    32             case 'POST':
    33                 curl_setopt($ci, CURLOPT_POST, TRUE);
    34                 if (!empty($params))
    35                 {
    36                     if($multi)
    37                     {
    38                         foreach($multi as $key => $file)
    39                         {
    40                             $params[$key] = '@' . $file;
    41                         }
    42                         curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
    43                         $headers[] = 'Expect: ';
    44                     }
    45                     else
    46                     {
    47                         curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
    48                     }
    49                 }
    50                 break;
    51             case 'DELETE':
    52             case 'GET':
    53                 $method == 'DELETE' && curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
    54                 if (!empty($params))
    55                 {
    56                     $url = $url . (strpos($url, '?') ? '&' : '?')
    57                         . (is_array($params) ? http_build_query($params) : $params);
    58                 }
    59                 break;
    60         }
    61         curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
    62         curl_setopt($ci, CURLOPT_URL, $url);
    63         if($headers)
    64         {
    65             curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
    66         }
    67 
    68         $response = curl_exec($ci);
    69         curl_close ($ci);
    70         return $response;
    71     }
    72 }
    如果感觉不错,请 一个!
    by simpman
  • 相关阅读:
    编写一个最原始的Servlet
    windows 通过cmd使用tail命令
    windows 配置jdk8环境变量
    Windows tomcat简单使用
    红黑树
    HashMap源码分析--jdk1.7
    IDEA debug断点调试技巧--转载
    idea 调试的时候变量的值变成了jar包显示
    浅谈Java中的hashcode方法--转载
    家庭记账本之微信小程序(八)
  • 原文地址:https://www.cnblogs.com/simpman/p/2880350.html
Copyright © 2011-2022 走看看