生活随笔
收集整理的這篇文章主要介紹了
发布一个http请求封装类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
調試時經常要模擬提交,在抓取別人頁面時也經常要去請求別人的頁面,于是就寫了下面這個類。封裝了三種post提交方法和一個request請求方法,
<?php????????????????????????class?HttpHelper?{??????????public?$ua_string=?"Mozilla/5.0?(Macintosh;?Intel?Mac?OS?X?10.7;?rv:14.0)?Gecko/20100101?Firefox/14.0.1";???????????public?$post_type_list?=?array("curl",?"socket",?"stream");???????????private?$cookie_file;?????????????????????public?function?__construct($params?=?array())?????{?????????if(count($params)?>?0)?????????{?????????????$this->init($params);?????????}?????}?????????????????????public?function?init($params)?????{?????????if(count($params)?>?0)?????????{?????????????foreach($params?as?$key?=>?$val)?????????????{?????????????????if(isset($this->$key))?????????????????{?????????????????????$this->$key?=?$val;?????????????????}?????????????}?????????}?????}???????????????????????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));?????}???????????????????????public?function?setUA($user_agent)?????{?????????$this->ua_string?=?$user_agent;?????????return?$this;?????}???????????????????????public?function?setCookieFile($cookie_file)?????{?????????$this->cookie_file?=?$cookie_file;?????????return?$this;?????}????????????????????????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;?????}??????????????????????????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;?????}????????????????????????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;?????}?}? 例子:
<?php?require_once?'HttpHelper.php';?$HttpHelper?=?new?HttpHelper();??$url?=?"http://localhost/post.php";??$data?=?array("name"=>"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);? post.php代碼如下:
<?php?echo?'test?request:';?print_r($_REQUEST);? 輸出結果:
Array?(?????[0]?=>?test?request:Array?(?????[name]?=>?socket?)??????[1]?=>?test?request:Array?(?????[name]?=>?curl?)??????[2]?=>?test?request:Array?(?????[name]?=>?stream?)??????[3]?=>?test?request:Array?(?)??)? ?
轉載于:https://blog.51cto.com/ustb80/1043797
總結
以上是生活随笔為你收集整理的发布一个http请求封装类的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。