PHP post & get请求
Posted 狗哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP post & get请求相关的知识,希望对你有一定的参考价值。
1 <?php 2 /** 3 * HTTP 请求类 4 */ 5 class HttpHelper { 6 7 const METHOD_GET = ‘GET‘; 8 const METHOD_POST = ‘POST‘; 9 10 private static $_connectTimeout = 60; 11 private static $_timeout = 60; 12 13 /** 14 * Http post请求 15 * @param type $url http url address 16 * @param type $data post params name => value 17 */ 18 public static function post($url, $data = array()){ 19 20 $queryString = self::buildHttpQueryString($data, self::METHOD_POST); 21 22 $response = self::makeHttpRequest($url, self::METHOD_POST,$queryString); 23 return $response; 24 } 25 26 /** 27 * http get 请求 28 * @param type $url http url address 29 * @param type $data get params name => value 30 */ 31 public static function get($url,$data = array()) { 32 if(!empty($data)){ 33 $url .= "?" . self::buildHttpQueryString($data, self::METHOD_GET); 34 } 35 $response = self::makeHttpRequest($url, self::METHOD_GET); 36 return $response; 37 } 38 39 /** 40 * 构造并发送一个http请求 41 * @param type $url 42 * @param type $method 43 * @param type $postFields 44 * @return type 45 */ 46 public static function makeHttpRequest($url,$method,$postFields = null) { 47 $ch = curl_init(); 48 curl_setopt($ch, CURLOPT_URL, $url); 49 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 50 if(self::METHOD_POST == $method){ 51 curl_setopt($ch, CURLOPT_POST, 1); 52 if(!empty($postFields)){ 53 curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); 54 } 55 } 56 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$_connectTimeout); 57 curl_setopt($ch, CURLOPT_TIMEOUT, self::$_timeout); 58 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 59 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 60 61 $result = curl_exec($ch); 62 curl_close($ch); 63 return $result; 64 } 65 66 /** 67 * 构造http请求的查询字符串 68 * @param array $params 69 * @param type $method 70 * @return string 71 */ 72 public static function buildHttpQueryString(array $params, $method = self::METHOD_GET) { 73 if(empty($params)){ 74 return ‘‘; 75 } 76 if(self::METHOD_GET == $method){ 77 $keys = array_keys($params); 78 $values = self::urlEncode(array_values($params)); 79 $params = array_combine($keys, $values); 80 } 81 82 $fields = array(); 83 84 foreach ($params as $key => $value) { 85 $fields[] = $key . ‘=‘ . $value; 86 } 87 return implode(‘&‘,$fields); 88 } 89 90 /** 91 * url encode 函数 92 * @param type $item 数组或者字符串类型 93 * @return type 94 */ 95 public static function urlEncode($item) { 96 if(is_array($item)){ 97 return array_map(array(‘HttpHelper‘,‘urlEncode‘), $item); 98 } 99 return rawurlencode($item); 100 } 101 102 /** 103 * url decode 函数 104 * @param type $item 数组或者字符串类型 105 * @return type 106 */ 107 public static function urlDecode($item){ 108 if(is_array($item)){ 109 return array_map(array(‘HttpHelper‘,‘urlDecode‘), $item); 110 } 111 return rawurldecode($item); 112 } 113 }
使用方法:
echo HttpHelper::get(‘http://www.baidu.com‘, array(‘t‘ => 1));
以上是关于PHP post & get请求的主要内容,如果未能解决你的问题,请参考以下文章
转:PHP中的使用curl发送请求(GET请求和POST请求)