分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

php socket 发送http请求

发布时间:2023-09-06 02:03责任编辑:胡小海关键词:http
<?php/** * Created by PhpStorm. * User: Mch * Date: 7/8/18 * Time: 21:39@func: parse_urlhttp://www.tfjyzx.com/model-school.jsp?area=%E5%BC%80%E5%B0%81&school=%E5%BC%80%E5%B0%81%E5%B8%82%E7%AC%AC%E4%BA%94%E4%B8%AD%E5%AD%A6#%E5%AD%A6%E6%A0%A1%E7%AE%80%E4%BB%8Barray(5) {["scheme"]=>string(4) "http"["host"]=>string(14) "www.tfjyzx.com"["path"]=>string(17) "/model-school.jsp"["query"]=>string(94) "area=%E5%BC%80%E5%B0%81&school=%E5%BC%80%E5%B0%81%E5%B8%82%E7%AC%AC%E4%BA%94%E4%B8%AD%E5%AD%A6"["fragment"]=>string(36) "%E5%AD%A6%E6%A0%A1%E7%AE%80%E4%BB%8B"}*/interface Proto { ???// 连接url ???public function conn($url); ???//发送get查询 ???public function get(); ???// 发送post查询 ???public function post(); ???// 关闭连接 ???public function close();}class Http implements Proto { ???const CRLF ?= "\r\n"; ???const BUFFSIZE = 1024; ???protected $errno = -1; ???protected $errstr = ‘‘; ???protected $response = ‘‘; ???protected $url = []; ???protected $version = ‘HTTP/1.1‘; ???protected $fh = null; ???protected $line = []; ???protected $header = []; ???protected $body = []; ???public function __construct($url) { ???????$this->conn($url); ???????$this->setHeader(‘Host: ‘ . $this->url[‘host‘]); ???} ???// POST /student/login HTTP/1.1 ???protected function setLine($method) { ???????$query = $this->url[‘query‘]; ???????$this->line[0] = implode(‘ ‘, [ ???????????$method, ???????????$this->url[‘path‘].(strlen($query)>0 ? ‘?‘.$query : ‘‘), ???????????$this->version ???????]); ???} ???public function setHeader($headerline) { ???????$this->header[] = $headerline; ???} ???public function setBody($body) { ???????$this->body[] = http_build_query($body); ???} ???// 连接url ???public function conn($url) { ???????$this->url = parse_url($url); ???????if(!isset($this->url[‘port‘])) { ???????????$this->url[‘port‘] = 80; ???????} ???????if(!isset($this->url[‘query‘])) { ???????????$this->url[‘query‘] = ‘‘; ???????} ???????$this->fh = fsockopen($this->url[‘host‘], ???????????$this->url[‘port‘], ???????????$this->errno, ???????????$this->errstr, ???????????3 ??/* timeout 3s */ ???????); ???????if (!$this->fh) { ???????????echo "$this->errstr ($this->errno)"; ???????????return NULL; ???????} ???????return $this->fh; ???} ???//构造get请求的数据 ???public function get() { ???????$this->setLine(‘GET‘); ???????$this->request(); ???????return $this->response; ???} ???// 构造post查询的数据 ???public function post($body = [], $enctype = ‘application/x-www-form-urlencoded‘) { ???????$this->setLine(‘POST‘); ???????// content-type ‘multipart/form-data‘ or ... ???????$this->setHeader(‘Content-type: ‘ . $enctype . ‘; charset=UTF-8‘); ???????$this->setBody($body); ???????$this->setHeader(‘Content-length: ‘ . strlen($this->body[0])); ???????$this->request(); ???????return $this->response; ???} ???// 关闭连接 ???public function close() { ???????$this->fh && fclose($this->fh); ???} ???// 真正请求 ???private function request() { ???????// 把请求行,头信息,实体信息 放在一个数组里,便于拼接 ???????fwrite($this->fh, implode(self::CRLF, array_merge( ???????????$this->line, ???????????$this->header, ???????????[‘‘], ???????????$this->body, ???????????[‘‘] ???????))); ???????// 为什么循环第2次时候很慢(假设响应长度<BUFFSIZE, 即1次读取完了)? ???????while(!feof($this->fh)) { ???????????$content = fread($this->fh, self::BUFFSIZE); ???????????if (strlen($content)<=0) { ???????????????break; ???????????} ???????????$this->response .= $content; ???????????// echo $this->response; ???????} ???}}// $url = "http://www.tfjyzx.com/news/listTVProgram?area=%E8%AE%B8%E6%98%8C";$url = "http://www.tfjyzx.com/student/login";$http = new Http($url);// $resp = $http->get();$http->setHeader(‘X-Requested-With: XMLHttpRequest‘);$http->setHeader(‘User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36‘);// !important $.ajax() contentType: ‘application/json‘$http->setHeader(‘Accept: application/json, text/javascript, */*; q=0.01‘);// ‘username=mingzhanghui&pwd=123456&captcha=&count=1‘$resp = $http->post([ ???‘username‘ => ‘mingzhanghui‘, ???‘pwd‘ => ‘123456‘, ???‘captcha‘ => ‘‘, ???‘count‘ => 1]);var_dump($http);$http->close();echo $resp;

  

php socket 发送http请求

原文地址:https://www.cnblogs.com/mingzhanghui/p/9281794.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved