两个php函数

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

1、http_build_query($params);

2、parse_str($r, $out);

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

 1 /**

 2  * HTTP请求类

 3  * @author xiaopengzhu <[email protected]>

 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 }

 

你可能感兴趣的:(PHP)