[关闭]
@a5635268 2015-10-10T18:24:51.000000Z 字数 2410 阅读 986

【PHPsocket编程专题(实战篇③)】构建基于socket的HTTP请求类

PHP


该代码是两年前写的,现在看起来有点渣了,仅仅是提供一个思路,现在做一些Api开发的时候官方会有一些SDK,这些SDK其实原理都是通过socket来通讯的,事实我个人主张用curl更方便,当然前提是你的主机上的PHP安装了此扩展

  1. <?php
  2. class Http{
  3. const CRLF = "\r\n";
  4. //把要拼接的内容放在数组里面最后用array_merge和import函数来拼接;
  5. private $line = array(); //请求行
  6. private $url = ''; //url;
  7. private $head = array(); //请求的主体;
  8. private $host = array(); //请求头的主机信息;
  9. private $urlInfo = array(); //pathInfo地址栏的url信息;
  10. private $query = ""; //pathInfo里面的query信息;
  11. private $body = array();
  12. private $fo = null; //socket资源;
  13. private $errno = -1; //socket资源打开的错误代码;
  14. private $errstr = "";//socket资源打开的错误描述;
  15. //public $response = "";//返回的响应字符串;
  16. public function __construct($url){
  17. $this->contact($url);
  18. }
  19. private function setLine($method){ //设置请求行
  20. $path = isset($this->urlInfo['path']) ? $this->urlInfo['path'] : "/";
  21. $this->line[] = $method . ' ' . $path . $this->query . ' ' . "HTTP/1.1";
  22. }
  23. public function setHead($content){ //设置请求头
  24. $this->head[] = $content;
  25. }
  26. private function setBody($data){ //请求主体
  27. $bodystr = '';//请求主体的内容;
  28. if (is_array($data)) {
  29. $bodystr = http_build_query($data);
  30. } else {
  31. $bodystr = $data;
  32. }
  33. $this->body[] = $bodystr;
  34. }
  35. private function contact($url){ //链接资源句柄
  36. $this->urlInfo = parse_url($url);
  37. $this->host[] = "Host: " . $this->urlInfo['host'];
  38. if (isset($this->urlInfo['query'])) {
  39. $this->query = "?" . $this->urlInfo['query'];
  40. } else {
  41. $this->query = "";
  42. }
  43. $port = isset($this->urlInfo['port']) ? $urlInfo['port'] : 80;//端口;
  44. $this->fo = fsockopen($this->urlInfo['host'] , $port , $this->errno , $this->errstr , 2);
  45. }
  46. public function post($data){ //发送post请求
  47. //这里的$data有可能是array的数组,也有可以能是key1=value1&key2=value2这样的字符串;
  48. $this->setLine("POST");
  49. $this->setHead("Content-Type: application/x-www-form-urlencoded");//注意这段话的大小写;
  50. $this->setBody($data);
  51. $strlen = strlen($this->body[0]);
  52. $this->setHead("Content-length: " . $strlen);
  53. $result = array_merge($this->line , $this->host , $this->head , array("") , $this->body , array(""));
  54. $request = implode(self::CRLF , $result);
  55. return $this->response($request);
  56. }
  57. public function get(){ //发送get请求
  58. $this->setLine("GET");
  59. $result = array_merge($this->line , $this->host , array("") , array(""));//特别注意,这个地方要空行再空行;
  60. $request = implode(self::CRLF , $result);
  61. return $this->response($request);
  62. }
  63. public function response($requestStr){//获取的响应资源;
  64. fputs($this->fo , $requestStr);
  65. $result = "";
  66. while (!feof($this->fo)) {
  67. $result .= fread($this->fo , 1024);
  68. }
  69. $this->close();
  70. return $result;
  71. }
  72. public function close(){
  73. fclose($this->fo);
  74. }
  75. }
  76. ?>
  77. ?>

核心函数:

  1. fsockopen(主机名称,端口号码,错误号的接受变量,错误提示的接受变量,超时时间)

通过fsockopen就可以打开一个socket通道,这时候可以使用fwrite或者fputs函数中的任意一个,把http的请求格式发给fsockopen()打开的句柄,这时候一个socket模拟的http请求就完成了.但是还得通过fread函数把响应头给取回来;

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注