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. 004 PCI Express体系结构(四)

    一.PCI总线的中断机制 PCI总线使用INTA#.INTB#.INTC#和INTD#信号向处理器发出中断请求.这些中断请求信号为低电平有效,并与处理器的中断控制器连接.在PCI体系结构中,这些中断信 ...

  2. java JNI介绍

    java JNI介绍 目录 java JNI介绍 1. Java调用C++代码 2.C++代码调用java代码 JNI是Java Native Interface的全称. oracle文档中是这样描述 ...

  3. NOIP 模拟 7 考试总结

    T1 超级大水题,用 \(kmp\) 和 \(hash\) 均能过,但都忘了,结果只打了个暴力.难受.板子题,题解就不放了 Code #include<bits/stdc++.h> #de ...

  4. 【AI】TorchVision_DataLoad

    From: https://liudongdong1.github.io/ All datasets are subclasses of torch.utils.data.Dataset i.e, t ...

  5. Quartz任务调度(3)存储与持久化操作配置详细解

    内存存储RAMJobStore Quartz默认使用RAMJobStore,它的优点是速度.因为所有的 Scheduler 信息都保存在计算机内存中,访问这些数据随着电脑而变快.而无须访问数据库或IO ...

  6. spring的异常处理

    出自于:https://blog.csdn.net/he90227/article/details/46309297   ---- 利用Spring进行统一异常处理的两种方式. 原文:https:// ...

  7. 徒手撸一个简单的RPC框架

    来源:https://juejin.im/post/5c4481a4f265da613438aec3 之前在牛逼哄哄的 RPC 框架,底层到底什么原理得知了RPC(远程过程调用)简单来说就是调用远程的 ...

  8. SpringCloud分布式配置中心Config

    统一管理所有配置. 1.微服务下的分布式配置中心 简介:讲解什么是配置中心及使用前后的好处 什么是配置中心: 一句话:统一管理配置, 快速切换各个环境的配置 相关产品: 百度的disconf 地址:h ...

  9. web.xml中自定义Listener

    Listener可以监听容器中某一执行动作,并根据其要求做出相应的响应. 常用的Web事件的监听接口如下: ServletContextListener:用于监听Web的启动及关闭 ServletCo ...

  10. 将svn项目导出,再导入到其他工作空间

    方法一: 对于一致svn地址,本地没有的项目,直接eclipse中svn检出即可. 若本地有项目,但想导入到另一个工作空间(即拷贝一份,不想再从svn拉),则需要用export方法. 方法二(expo ...