php CURL 发送http请求 GET POST
* CURL
http://www.php.net/manual/en/book.curl.php
http://jp2.php.net/manual/en/function.curl-setopt.php
GET:
<?php
/**
* Created by PhpStorm.
* User: Mch
* Date: 7/8/18
* Time: 16:02
*/
$ch = curl_init(); $url = 'http://www.tfjyzx.com/news/listTeacherByArea';
$params = [
'area' => '开封市',
'limit' => 6,
'type' => '学生'
]; function get_url($url, $params) {
$a = [];
foreach ($params as $name => $value) {
$a[] = $name .'=' .urlencode($value);
}
$url .= '?'.implode('&', $a);
return $url;
} $url = get_url($url, $params);
echo $url.PHP_EOL; curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1
]); $data = curl_exec($ch);
curl_close($ch); echo $data.PHP_EOL;
http://www.tfjyzx.com/news/listTeacherByArea?area=%E5%BC%80%E5%B0%81%E5%B8%82&limit=6&type=%E5%AD%A6%E7%94%9F
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: zh
Transfer-Encoding: chunked
Date: Sun, 08 Jul 2018 09:30:23 GMT
{"teacherList":[{"id":43,"name":"杜姝臻","url":null,"img":"./kaifeng33_img/studentView/4.jpg","school":"开封市33中","datetime":null,"intro":"三十三中优秀学生代表发言","content":null},{"id":42,"name":"朱彤","url":null,"img":"./kaifeng33_img/studentView/3.jpg","school":"开封市33中","datetime":null,"intro":"初二七班","content":null},{"id":41,"name":"张梦岩","url":null,"img":"./kaifeng33_img/studentView/2.jpg","school":"开封市33中","datetime":null,"intro":"三六班","content":null},{"id":40,"name":"周梦寒","url":null,"img":"./kaifeng33_img/studentView/1.jpg","school":"开封市33中","datetime":null,"intro":"三六班","content":null},{"id":20,"name":"程园林","url":null,"img":"./publish/students/kaifeng5/04.jpg","school":"开封市五中","datetime":null,"intro":"15届高三四","content":null},{"id":19,"name":"朱崇","url":null,"img":"./publish/students/kaifeng5/03.jpg","school":"开封市五中","datetime":null,"intro":"15届高三四 朱崇","content":null}]}
POST:
<?php
/**
* Created by PhpStorm.
* User: Mch
* Date: 7/8/18
* Time: 16:19
*/
$ch = curl_init(); $s = "POST /student/login HTTP/1.1
Host: www.tfjyzx.com
Connection: keep-alive
Content-Length: 48
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/javascript; q=0.01
Origin: http://www.tfjyzx.com
X-Requested-With: XMLHttpRequest
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
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://www.tfjyzx.com/login.jsp
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6
Cookie: experience=show; JSESSIONID=C63BF22874A3B791F212CFCBAFFAE432";
$header = explode("\n", $s); // http://jp2.php.net/manual/en/function.curl-setopt.php
curl_setopt_array($ch, [
CURLOPT_URL => 'http://118.190.150.189/student/login',
// TRUE to include the header in the output.
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
// TRUE to do a regular HTTP POST. This POST is the normal
// application/x-www-form-urlencoded kind, most commonly used by HTML forms.
CURLOPT_POST => 1,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_SAFE_UPLOAD => 1,
CURLOPT_HTTPHEADER => $header,
CURLOPT_POSTFIELDS => 'username=XXXXX150835&pwd=123456&captcha=&count=1', // phone number
/*
CURLOPT_POSTFIELDS => [
'username' => 'gmy12345',
'pwd' => '123456',
'captcha' => '',
'count' => 1
],
CURLOPT_ENCODING => 'gzip, deflate',
CURLOPT_COOKIE => 'experience=show; JSESSIONID=C63BF22874A3B791F212CFCBAFFAE432',
*/
CURLOPT_TIMEOUT => 5
]); $data = curl_exec($ch);
curl_close($ch); echo $data . PHP_EOL;
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=AC471F40CDC460D6D7C14BAACAE21ED5; Path=/; HttpOnly
Content-Type: application/json;charset=UTF-8
Content-Length: 118
Date: Sun, 08 Jul 2018 09:32:30 GMT
{"result":1,"cause":"登录成功!","school":"濮阳市第八中学","sessionId":"AC471F40CDC460D6D7C14BAACAE21ED5"}
Sample Request Header:
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8,ja;q=0.6,en;q=0.4,ko;q=0.2,en-US;q=0.2,vi;q=0.2,fr;q=0.2,la;q=0.2
Connection:keep-alive
Host:192.168.10.137:9999
Origin:http://zhanghum:8088
Referer:http://zhanghum:8088/admin/index.html
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Sample Response header:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://zhanghum:8088
Content-Encoding:gzip
Content-Type:application/json
Date:Mon, 16 Jul 2018 02:03:12 GMT
Transfer-Encoding:chunked
Vary:Accept-Encoding
php CURL 发送http请求 GET POST的更多相关文章
- curl 发送post请求
curl 发送post请求 curl -X POST "http://localhost:8080/usr3?id=1&name=3&departmentId=2" ...
- curl 发送json请求
curl 发送json请求 这个是在cmd环境下的输入:注意{\"userName\":\"helo\",\"id\":1}中间不能有空格 ...
- linux shell中curl 发送post请求json格式问题
今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...
- 每天一个linux命令13之curl发送http请求
一.get请求 curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...
- [转]使用 curl 发送 POST 请求的几种方式
HTTP 的 POST 请求通常是用于提交数据,可以通过这篇文章来了解各种提交方式:四种常见的 POST 提交数据方式.做 Web 后端开发时,不可避免地要自己给自己发请求来调试接口,这里要记录的内容 ...
- curl发送post请求,统计响应时间
curl -o /dev/null -s -w %{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_total}:: ...
- linux用curl发送post请求
1.curl -X POST “http://XXXXXXX”这种请求方式参数直接写在URL里面的,而不是body
- 一个常用的通过curl发送HTTP请求的函数
function: function curl_get($url, $params) { return curl_http($url, $params, 'GET'); } function curl ...
- php curl 发送post请求
PHP curl_init函数 resource curl_init ([ string $url = NULL ] ) 初始化一个新的会话,返回一个cURL句柄,供curl_setopt(), cu ...
- curl 发送 post 请求
curl -i -X POST -H 'Content-type':'application/json' -d '{"keyWord":"雅诗兰黛"," ...
随机推荐
- Sqli-Labs less26-28a
less-26 从第26关开始,我的sqli-labs就在docker上运行了,因为windows中阿帕奇对空格的转义有问题 通过源码可以看到有很多过滤,包括空格 or和and. 方法: or可以用| ...
- Java序列化bean保存到本地文件中
File file = new File("D:\\softTemp\\student.out"); ObjectOutputStream objectOutputStream = ...
- vue中常用插件(货币、日期)
货币插件: 价格格式化 // https://github.com/vuejs/vuex/blob/dev/examples/shopping-cart/currency.js const digit ...
- mysql 局域网连接
下面分别简述操作: 配置虚拟机网络 默认方式是NAT,但为了让宿主机之外的其它计算机也能访问虚拟机,NAT方式配置起来有些复杂,这里推荐用桥接模式,关于VM的几种网络方式的区别,可以参考这篇文章配置好 ...
- 踩坑记录--接口调用,传参DataTable报错
问题描述 服务端提供接口,接口参数包含DataTable类型,客户端调用显示请求报错,Postman调用显示Could not get response 解决 原因 接口实现基于wcf,而wcf参数类 ...
- 【springcloud】Eureka服务注册中心搭建
转自:https://blog.csdn.net/pengjunlee/article/details/86538997 Spring Cloud是一系列框架的集合,它利用Spring Boot的开发 ...
- ARP:地址解析协议
我们假设这样一个场景:你需要和你网络中的一个设备进行通信,这个设备可能是某种服务器.你用来创建这个通信的应用已经得到了这个远程主机的ip地址,也意味着系统已经有了用来构建它想要在第三层到第7层传递数据 ...
- Javascript - 异步操作和读取文件
node.js读取文件 node.js内置了异步读取文件的模块,可以很方便地读取文件的数据.先创建三个txt文档,在根目录下创建一个readFile.js 输入以下代码,然后在vscode的终端中输入 ...
- vue 中this.$on 为什么要放在created中?
最近在思考一个问题为什么一定要在created中写this.$on,可以放在mounted中吗 如果触发和监听组件在页面上都创建了,那么可以放在mounted中 这种情况在实际工作中比较常见,如果在触 ...
- C# - Timer 实现跑马灯