php 함수 두 개

11612 단어 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 }

좋은 웹페이지 즐겨찾기