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":"雅诗兰黛"," ...
随机推荐
- 【工作篇】了解升级 Spring 版本导致的跨域问题
一.背景 最近需要统一升级 Spring 的版本,避免 common 包和各个项目间的 Spring 版本冲突问题.这次升级主要是从 Spring 4.1.9.RELEASE 升级到 Spring 4 ...
- PS与CSS字间距转换
字间距: 实际像素大小 real_letter_spacing,(单位为px) 文字字号 font,(单位为px) 文字间距 spacing, 它们的换算关系为: real_letter_spacin ...
- 浅看spa单页应用路由
路由观察浏览器的URL的变更.当URL 变更时,路由会解析它并生成一个新的路由实例. 一个基本的路由是这样的: class Router { private _defaultController: s ...
- RestTemplate post请求 Controller 接收不到值的解决方案 postForObject方法源码解析
springboot 整合 RestTemplate 与 使用方法 RestTemplate 的 postForObject 方法有四个参数 String url => 顾名思义 这个参数是请求 ...
- Maven项目管理工具--简单实用与入门
Maven管理的方式就是"自动下载项目所需要的jar包,统一管理jar包之间的依赖关系" Maven下载与安装 1.首先确保JDK已安装,且JDK为1.6+(尽量新,新肯定支持,旧 ...
- 多台服务器共享session问题(2)
多台服务器共享session问题 转载自:https://www.cnblogs.com/lingshao/p/5580287.html 在现在的大型网站中,如何实现多台服务器中的session数据 ...
- Java知识图谱(附:阿里Java学习计划)
摘要: 本文主要描绘了Java基础学习过程,给出Java知识结构图,以及阿里Java岗学习计划,对Java学习爱好者.准备及将要从事Java开发方面的同学大有裨益. 温馨提示: 由于C ...
- docker安装与配置redis详细过程
注:大鸟飞过,这只是简单搭建,能快速运用而已!! 第一步 pull redis 命令:docker pull redis 第二步 创建redis管理目录,方便后期管理 命令: mkdir /data/ ...
- Ubuntu 16.04LTS安装flashplayer
转载自http://www.linuxdiyf.com/linux/20084.html 在安装Ubuntu 16.04LTS后,播放有视频的网页时,总提示你要安装缺失的插件,在 ubuntu 系统下 ...
- .NetCore3.1获取文件并重新命名以及大批量更新及写入数据
using Microsoft.AspNetCore.Mvc; using MySql.Data.MySqlClient; using System; using System.Collections ...