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. Spring-Boot注入自定义properties文件配置

    创建wzq.properties wzq.properties注入User实体类中 @PropertySource(value = "classpath:wzq.properties&quo ...

  2. 【mysql】截取查询分析

    1. 慢查询日志 1.1 是什么 (1)MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL ...

  3. 面试题:hashcode相等两个类一定相等吗?equals呢?相反呢?

    首先如果hashcode相等的话,这两个类也是不一定相等的,如果是反过来的话(通常情况下,如果两个对象的内容相同,两个对象的hashcode也是相同的) hashcode()和equals()的关系: ...

  4. 【java web】监听器listener

    一.简介 Java的监听器,也是系统级别的监听.监听器随web应用的启动而启动.Java的监听器在c/s模式里面经常用到,它会对特定的事件产生产生一个处理.监听在很多模式下用到,比如说观察者模式,就是 ...

  5. WPF 勾选划线

    最近项目需要一个左右侧一对多的划线功能 我们先来看一下效果秃: 主要功能: 支持动态添加 支持复选 支持修改颜色 支持动态宽度 主要实现:事件的传递 应用场景:购物互选,食品搭配,角色互选 数据源 左 ...

  6. 何时覆盖hashCode()和equals()方法

    The theory (for the language lawyers and the mathematically inclined): equals() (javadoc) must defin ...

  7. 【转】时冲的CSDN:Linux系统各个目录的作用

    请各位移步原文链接:时冲的CSDN 以下仅用于个人梳理,排版方便阅读记忆(原文更优): from my typora: 文章目录 Linux文件系统 LINUX有四种基本文件系统类型: 1.普通文件: ...

  8. spring动态切换数据源(一)

    介绍下spring数据源连接的源码类:| 1 spring动态切换连接池需要类AbstractRoutingDataSource的源码 2 /* 3 * Copyright 2002-2017 the ...

  9. Learning ROS: Getting started with roswtf (检查ROS系统,找出问题)

    本文主要部分来源于ROS官网的Tutorials. roswtf是ROS的检查工具,用于检查ROS安装和运行系统. Checking your installation&Offline mak ...

  10. SpringBoot笔记(2)

    一.容器功能 1.1 组件添加 1. @Configuration Full模式:获取对象时,首先在容器内搜索是否存在,如存在直接拿出 默认为Full模式,单例 配置类组件之间有依赖关系,方法会被调用 ...