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. SIM900A—发送、接收中英文短信

    文章目录 一.SMS简介 二.短信的控制模式与编码 1.Text Mode 2.PDU Mode 3.GSM编码 4.UCS2编码 三.收发英文短信 1.AT+CPMS查询短信数量 2.AT+CNMI ...

  2. RabbitMQ和Elasticsearch的使用笔记

    Demo介绍 学习rabbitmq和elasticsearch后的小练习,主要功能点介绍: 1.elasticsearch实现搜索.条件查询和分页: 2.搜索周边酒店信息 3.酒店竞价排名: 4.后台 ...

  3. 干货!4大实验项目,深度解析Tag在可观测性领域的最佳实践!

    Opentelemetry协议,是CNCF(Cloud Native Computing Foundation-云原生计算基金会)定义的最新一代的可观测规范(目前还在孵化中),该规范定义了可观测性的三 ...

  4. AECC2018同时中英文切换多开使用,加倍提高你的工作效率

    最近相信不少人已经更新了AECC2018,升级之后第一件重要的事当然是中英文的切换了,要不然工作中很麻烦.对于一直习惯用中文的人来说,在用模板过程中会出现各种表达式报错极其不方便,而对于习惯英文操作朋 ...

  5. .Net Core 集成 Redis

    首先安装RedisServer 安装教程可参照 http://www.redis.cn/download.html 或者 https://www.runoob.com/redis/redis-inst ...

  6. react启动报babel-eslint依赖版本不一致

    遇到的问题: gitlab上拉的react的项目,下载依赖之后,yarn start启动报错 因为按他说的步骤:1.改动比较大, 2.完全没什么作用(重点) 解决方案:直接忽略,如果项目没有.env文 ...

  7. CrackMe-Cycle

    转载自 OllyDBG入门教程   PS:自己逆在 最后的时候总会崩,不知道为什么. 我们输入用户名 CCDebuger,序列号 78787878,点上面那个"Check"按钮,呵 ...

  8. java 循环移位输出全排列

    //题目:利用1.2.2.3.4这4个数字,用java写一个main函数打印出所有不同的排列,如12234,,2234等,要求打印出来不能有重复 1 package test123; 2 3 impo ...

  9. C# 使用正则表达式替换PPT中的文本(附vb.net代码)

    文本介绍如何在C#程序中使用正则表达式替换PPT幻灯片中的指定文本内容.具体操作步骤如下: 1. 在程序中引用Spire.Presentation.dll.两种方法可参考如下: (1)直接在程序中通过 ...

  10. 项目版本管理Git使用详细教程

    前言 记得刚开始做项目开发的时候都是一个人完成一个项目,单打独斗的开发,也不知道什么是团队开发,没有这个概念,随着工作后来知道公司里项目都是团队开发,这个时候这么多人怎么开发一个项目呢,难道用u盘拷贝 ...