PHP如何实现Post和Get请求处理?

2026-04-03 05:381阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计375个文字,预计阅读时间需要2分钟。

PHP如何实现Post和Get请求处理?

封装PHP+cUrl库,主要提供post、get方法,支持cookie和session。

phpclass CurlHelper{ private $curl;

public function __construct() { $this->curl=curl_init(); $this->_setopt(CURLOPT_RETURNTRANSFER, true); $this->_setopt(CURLOPT_HEADER, true); $this->_setopt(CURLOPT_FOLLOWLOCATION, true); }

public function post($url) { // 实现post方法 }

PHP如何实现Post和Get请求处理?

public function get($url) { // 实现get方法 }

private function setopt($option, $value) { curl_setopt($this->curl, $option, $value); }

public function close() { curl_close($this->curl); }}

封装PHP cUrl库。主要提供post、get方法,支持cookie,session

curl = curl_init(); $this->setopt(CURLOPT_RETURNTRANSFER, true); $this->setopt(CURLOPT_HEADER, true); $this->setopt(CURLOPT_FOLLOWLOCATION, true); } public function post($url, $data, $cookie = '', &$session = false, &$code = false){ $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_POST, true); $this->setopt(CURLOPT_POSTFIELDS, $data); $this->setopt(CURLOPT_COOKIE, $cookie); $out = $this->exec(); $arr = $this->cutHeadAndBody($out); $code = $this->getStart($arr['head']); $session = $this->getCookie($arr['head']); return $arr['body']; } public function get($url, $cookie = '', &$session = false, &$code = false){ $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_POST, false); $this->setopt(CURLOPT_COOKIE, $cookie); $out = $this->exec(); $arr = $this->cutHeadAndBody($out); $code = $this->getStart($arr['head']); $session = $this->getCookie($arr['head']); return $arr['body']; } public function setopt($key, $value){ curl_setopt($this->curl, $key, $value); } public function exec(){ return curl_exec($this->curl); } function __destruct(){ curl_close($this->curl); } //分割出返回头和html主体 private function cutHeadAndBody($content){ $arr = explode($this->HTML,$content,2); $newArray = ['head'=>$arr[0],'body'=>$arr[0]]; if(count($arr) > 1) $newArray['body'] = $this->HTML.$arr[1]; return $newArray; } //取出200状态码,除200外其他状态码可能取不出,待优化 private function getStart($head){ $arr = explode("\n", $head); $newCode = false; foreach($arr as $list){ if(preg_match('/^HTTP.*\ ([0-9]*)\ OK/i', $list, $code) > 0){ $newCode = $code[1]; } } return $newCode; } //取出返回的cookie private function getCookie($head){ $cookie = ''; $preg = '/^set\-cookie:([^\r\n]*)/im'; $num = preg_match_all($preg, $head, $out); foreach($out[1] as $list){ $cookie .= $list.';' ; } return $cookie; } }

本文共计375个文字,预计阅读时间需要2分钟。

PHP如何实现Post和Get请求处理?

封装PHP+cUrl库,主要提供post、get方法,支持cookie和session。

phpclass CurlHelper{ private $curl;

public function __construct() { $this->curl=curl_init(); $this->_setopt(CURLOPT_RETURNTRANSFER, true); $this->_setopt(CURLOPT_HEADER, true); $this->_setopt(CURLOPT_FOLLOWLOCATION, true); }

public function post($url) { // 实现post方法 }

PHP如何实现Post和Get请求处理?

public function get($url) { // 实现get方法 }

private function setopt($option, $value) { curl_setopt($this->curl, $option, $value); }

public function close() { curl_close($this->curl); }}

封装PHP cUrl库。主要提供post、get方法,支持cookie,session

curl = curl_init(); $this->setopt(CURLOPT_RETURNTRANSFER, true); $this->setopt(CURLOPT_HEADER, true); $this->setopt(CURLOPT_FOLLOWLOCATION, true); } public function post($url, $data, $cookie = '', &$session = false, &$code = false){ $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_POST, true); $this->setopt(CURLOPT_POSTFIELDS, $data); $this->setopt(CURLOPT_COOKIE, $cookie); $out = $this->exec(); $arr = $this->cutHeadAndBody($out); $code = $this->getStart($arr['head']); $session = $this->getCookie($arr['head']); return $arr['body']; } public function get($url, $cookie = '', &$session = false, &$code = false){ $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_POST, false); $this->setopt(CURLOPT_COOKIE, $cookie); $out = $this->exec(); $arr = $this->cutHeadAndBody($out); $code = $this->getStart($arr['head']); $session = $this->getCookie($arr['head']); return $arr['body']; } public function setopt($key, $value){ curl_setopt($this->curl, $key, $value); } public function exec(){ return curl_exec($this->curl); } function __destruct(){ curl_close($this->curl); } //分割出返回头和html主体 private function cutHeadAndBody($content){ $arr = explode($this->HTML,$content,2); $newArray = ['head'=>$arr[0],'body'=>$arr[0]]; if(count($arr) > 1) $newArray['body'] = $this->HTML.$arr[1]; return $newArray; } //取出200状态码,除200外其他状态码可能取不出,待优化 private function getStart($head){ $arr = explode("\n", $head); $newCode = false; foreach($arr as $list){ if(preg_match('/^HTTP.*\ ([0-9]*)\ OK/i', $list, $code) > 0){ $newCode = $code[1]; } } return $newCode; } //取出返回的cookie private function getCookie($head){ $cookie = ''; $preg = '/^set\-cookie:([^\r\n]*)/im'; $num = preg_match_all($preg, $head, $out); foreach($out[1] as $list){ $cookie .= $list.';' ; } return $cookie; } }