php socket 发送http请求 GET POST
http://docs.php-http.org/en/latest/httplug/users.html
<?php
/**
* Created by PhpStorm.
* User: Mch
* Date: 7/8/18
* Time: 21:39
*/ 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;
@func: parse_url
http://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%8B array(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"
}
可以使用现有的库: php-http
php socket 发送http请求 GET POST的更多相关文章
- 使用socket发送http请求(get/post)
手动发送http请求 解释说明 https://blog.csdn.net/zhangliang_571/article/details/23508953 http://www.cnblogs.com ...
- c/c++ socket发送http请求访问网站
这几天课比较少,校园网上网要认证才能上网,每次必须输入学号密码,为了方便,写了一个自动登录以及如果在线,登录自服务系统强制下线的小工具. 强制下线思路:获取sessionID----------> ...
- PHP + Socket 发送http请求进而实现站点灌水
本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...
- 【C语言】Socket发送HTTP-TCP请求,数据有字符串插入
问题描述: 场景:编写Socket接口,向LOKI发送POST请求查询数据 BUG发现位置:通过cJSON读取时间戳,发现被截断. 现象:通过read()去读取返回的数据,数据行中被插入字符:如下 c ...
- C#用SOCKET发送HTTP请求小例
private void button1_Click(object sender, EventArgs e) { string urlStr = this.textUrl.Text ; if (url ...
- linux c 使用socket 发送http请求 可以发送json格式数据
#include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <time.h ...
- perl6 Socket: 发送HTTP请求
sub MAIN(Str $host,Str $path, Int $port) { my $send = "GET $path HTTP/1.1\r\nHost: $host\r\n\r\ ...
- php socket 发送HTTP请求 POST json
* HttpRequest.php <?php namespace et\http; /** * Created by PhpStorm. * User: mingzhanghui * Date ...
- socket发送http请求
随机推荐
- Linux添加防火墙、iptables的安装和配置
由于centos7默认是使用firewall作为防火墙,下面介绍如何将系统的防火墙设置为iptables. #停止firewall systemctl stop firewall.service #禁 ...
- C# 获得文件的执行路径的方法
var path = System.Reflection.Assembly.GetEntryAssembly().Location;
- Mysql---mysqldump参数详细说明(转)
Mysqldump参数大全(参数来源于mysql5.5.19源码) mysqldump.exe一般会默认安装在C:\Program Files\MySQL\MySQL Server 5.5\bin 参 ...
- sizeof()和 strlen()的区别 --- 个人笔记
在学习C语言和linux的时候,遇到了一些常见问题.题目,有些很简单,有些容易出错,本人水平有限,未免会出错,今天有时间,就将以前做的笔记,一一拿出来,写写blog. sizeof()和 strlen ...
- linux 常用命令(一)——查看硬盘空间-内存-线程的cpu负载-线程内存
系统参数检查: df -h [enter] 检查硬盘空间 TIP: 使用 man df 可查看该命令使用说明 ; q 退出. free检查内存使用情况: free [enter] TIP: 使用 ma ...
- 源码解析.Net中DependencyInjection的实现
前言 笔者的这篇文章和上篇文章思路一样,不注重依赖注入的使用方法,更加注重源码的实现,我尽量的表达清楚内容,让读者能够真正的学到东西.如果有不太清楚依赖注入是什么或怎么在.Net项目中使用的话,请点击 ...
- indexedDB数据库完整创建流程
1.打开数据库 使用 IndexedDB 的第一步是打开数据库,使用indexedDB.open()方法 var request = window.indexedDB.open(databaseNam ...
- TDSQL MySQL版基本原理-水平分表 读写分离 弹性扩展 强同步
TDSQL MySQL版(TDSQL for MySQL)是部署在腾讯云上的一种支持自动水平拆分.Shared Nothing 架构的分布式数据库.TDSQL MySQL版 即业务获取的是完整的逻辑库 ...
- Spring Boot 入门系列(二十三)整合Mybatis,实现多数据源配置!
d之前介绍了Spring Boot 整合mybatis 使用注解方式配置的方式实现增删改查以及一些复杂自定义的sql 语句 .想必大家对spring boot 项目中,如何使用mybatis 有了一定 ...
- Qt中QOpengl的QMatrix4x4矩阵作用原理以及使用方法
1.矩阵具有坐标变换的作用,例如:左乘一个旋转矩阵,实现点的坐标旋转,左乘一个平移矩阵实现,点的平移 2.一个点可以同时串联相乘几个变换矩阵,实现坐标连续变换,根据左乘规则,右边矩阵先作用于点,作用顺 ...