您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php写的http请求封装类

2025/12/2 16:42:46发布27次查看
本文介绍一个php实现的http请求封装类,封装了三种post提交方法和一个request请求方法,并提供了调用示例,供大家学习参考。在php编程调试时,经常需要模拟提交。另外,在抓取一些页面时,需要经常请求别人的页面。
于是实现了一个http请求的封装类,以方便调用。本http请求类,封装了三种post提交方法和一个request请求方法。
1,http请求封闭类
0) { $this->init($params); } } // ----------------------- /** * 参数初始化 * * @param array $params */ public function init($params) { if(count($params) > 0) { foreach($params as $key => $val) { if(isset($this->$key)) { $this->$key = $val; } } } } // ----------------------- /** * 提交请求 * * @param string $url 请求地址 * @param mixed $data 提交的数据 * @param string $type 提交类型,curl,socket,stream可选 */ public function post($url, $data, $type = socket) { if(!in_array($type, $this->post_type_list)) { die(undefined post type); } $function_name = $type . post; return call_user_func_array(array($this, $function_name), array($url, $data)); } // ----------------------- /** * 更改默认的ua信息 * * 本方法常用于模拟各种浏览器 * * @param string $ua_string ua字符串 */ public function setua($user_agent) { $this->ua_string = $user_agent; return $this; } // ----------------------- /** * 设置本地cookie文件 * * 在用curl来模拟时常需要设置此项 * * @param string $cookie_file 文件路径 */ public function setcookiefile($cookie_file) { $this->cookie_file = $cookie_file; return $this; } // ----------------------- /** * curl方式提交 * * @param string $url 请求地址 * @param mixed $data 提交的数据 * @param string $user_agent 自定义的ua * @return mixed */ public function curlpost($url, $data, $user_agent = '') { if($user_agent == '') { $user_agent = $this->ua_string; } if (!is_array($data)) { $data = array($data); } $data = http_build_query($data); if (!function_exists(curl_init)) { die('undefined function curl_init'); } $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_postfields, $data); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_useragent, $user_agent); $rs = curl_exec($ch); curl_close($ch); return $rs; } // ----------------------- /** * 套接字提交 * * @param string $url 请求地址 * @param mixed $data 提交的数据 * @param string $user_agent 自定义的ua * @param int $port 端口 * @param int $timeout 超时限制 * @return mixed */ public function socketpost($url, $data, $user_agent = '', $port = 80, $timeout = 30) { $url_info = parse_url($url); $remote_server = $url_info['host']; $remote_path = $url_info['path']; $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout); if(!$socket) { die($errstr($errno)); } if($user_agent == '') { $user_agent = $this->ua_string; } if (!is_array($data)) { $data = array($data); } $data = http_build_query($data); fwrite($socket, post {$remote_path} http/1.0\r\n); fwrite($socket, user-agent: {$user_agent}\r\n); fwrite($socket, host: {$remote_server}\r\n); fwrite($socket, content-type: application/x-www-form-urlencoded\r\n); fwrite($socket, content-length: . strlen($data) . \r\n); fwrite($socket, accept:*/*\r\n); fwrite($socket, \r\n); fwrite($socket, {$data}\r\n); fwrite($socket, \r\n); $header = ; while($str = trim(fgets($socket, 4096))) { $header .= $str; } $data = ; while(!feof($socket)) { $data .= fgets($socket, 4096); } return $data; } // ----------------------- /** * 文件流提交 * * @param string $url 提交地址 * @param string $data 数据 * @param string $user_agent 自定义的ua * @return mixed */ public function streampost($url, $data, $user_agent = '') { if($user_agent == '') { $user_agent = $this->ua_string; } if (!is_array($data)) { $data = array($data); } $data = http_build_query($data); $context = array( 'http' => array( 'method' => 'post', 'header' => 'content-type: application/x-www-form-urlencoded' . \r\n . 'user-agent : ' . $user_agent . \r\n . 'content-length: ' . strlen($data), 'content' => $data ) ); $stream_context = stream_context_create($context); $data = file_get_contents($url, false, $stream_context); return $data; } // ----------------------- /** * 发送请求 * * 本方法通过curl函数向目标服务器发送请求 * * @param string $url 请求地址 * @return mixed */ public function request($url) { $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_useragent, !empty($this->ua_string)? $this->ua_string : $_server['http_user_agent']); curl_setopt($ch, curlopt_followlocation, 1); if (isset($this->cookie_file)) { curl_setopt($ch, curlopt_cookiejar, $this->cookie_file); } $data = curl_exec($ch); curl_close($ch); return $data; } }?>
调用示例:
socket); $rs[] = $httphelper->post($url, $data); $data = array(name=>curl); $rs[] = $httphelper->post($url, $data, curl); $data = array(name=>stream); $rs[] = $httphelper->post($url, $data, stream); $rs[] = $httphelper->request($url); print_r($rs);?>
3,post.php文件:

4,输出结果:
array ( [0] => test request:array ( [name] => socket )
[1] => test request:array ( [name] => curl )
[2] => test request:array ( [name] => stream )
[3] => test request:array ( )
)
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product