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的更多相关文章

  1. 使用socket发送http请求(get/post)

    手动发送http请求 解释说明 https://blog.csdn.net/zhangliang_571/article/details/23508953 http://www.cnblogs.com ...

  2. c/c++ socket发送http请求访问网站

    这几天课比较少,校园网上网要认证才能上网,每次必须输入学号密码,为了方便,写了一个自动登录以及如果在线,登录自服务系统强制下线的小工具. 强制下线思路:获取sessionID----------> ...

  3. PHP + Socket 发送http请求进而实现站点灌水

    本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...

  4. 【C语言】Socket发送HTTP-TCP请求,数据有字符串插入

    问题描述: 场景:编写Socket接口,向LOKI发送POST请求查询数据 BUG发现位置:通过cJSON读取时间戳,发现被截断. 现象:通过read()去读取返回的数据,数据行中被插入字符:如下 c ...

  5. C#用SOCKET发送HTTP请求小例

    private void button1_Click(object sender, EventArgs e) { string urlStr = this.textUrl.Text ; if (url ...

  6. linux c 使用socket 发送http请求 可以发送json格式数据

    #include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <time.h ...

  7. perl6 Socket: 发送HTTP请求

    sub MAIN(Str $host,Str $path, Int $port) { my $send = "GET $path HTTP/1.1\r\nHost: $host\r\n\r\ ...

  8. php socket 发送HTTP请求 POST json

    * HttpRequest.php <?php namespace et\http; /** * Created by PhpStorm. * User: mingzhanghui * Date ...

  9. socket发送http请求

随机推荐

  1. 线程队列 concurrent 协程 greenlet gevent

    死锁问题 所谓死锁:是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进 ...

  2. 服务器受到网络攻击时,如何获取请求客户端的真实 IP?

    网络攻击 前不久公司遭受了一次网络攻击. 早晨刚到公司,就发现登录接口的调用次数飙升,很快就确认是被恶意攻击,让安全部门做网关入口针对对方 IP 加了限制. 并统一对所有的 IP 加了调用的频率限制. ...

  3. SpringDataJpa使用原生sql(EntityManager)动态拼接,分页查询

    SpringDataJpa Spring Data JPA是较大的Spring Data系列的一部分,可轻松实现基于JPA的存储库.该模块处理对基于JPA的数据访问层的增强支持.它使构建使用数据访问技 ...

  4. Python - 面向对象编程 - 实例方法、静态方法、类方法

    实例方法 在类中定义的方法默认都是实例方法,前面几篇文章已经大量使用到实例方法 实例方法栗子 class PoloBlog: def __init__(self, name, age): print( ...

  5. mysql8.0----mysqldump抛出:Unknown table 'COLUMN_STATISTICS' in information_schema (1109)

    问题:我尝试使用mysqldump时,得到以下错误: 复制 $> mysqldump --single-transaction --h  -u user -p db > db.sql my ...

  6. 发布日志 - kratos v2.0.4 版本发布

    V2.0.4 Release Release v2.0.4 · go-kratos/kratos (github.com) 新的功能 proto-gen-http 工具在生产代码时如果 POST/PU ...

  7. (三)羽夏看C语言——进制

    写在前面   由于此系列是本人一个字一个字码出来的,包括示例和实验截图.本人非计算机专业,可能对本教程涉及的事物没有了解的足够深入,如有错误,欢迎批评指正. 如有好的建议,欢迎反馈.码字不易,如果本篇 ...

  8. 【SpringMVC】文件上传与下载、拦截器、异常处理器

    文件下载 使用ResponseEntity实现下载文件的功能 index.html <!DOCTYPE html> <html lang="en" xmlns:t ...

  9. HashMap 为什么线程不安全?

    作者:developer http://cnblogs.com/developer_chan/p/10450908.html 我们都知道HashMap是线程不安全的,在多线程环境中不建议使用,但是其线 ...

  10. Redis的安装、基本使用以及与SpringBoot的整合

    1.概述 Redis 是现在很流行的一个 NoSql 数据库,每秒读取可以达到10万次,能够将数据持久化,支持多种数据结构,容灾性强,易扩展,常用于项目的缓存中间件. 今天我们就来聊聊关于Redis的 ...