* CURL 参照博客: http://www.cnblogs.com/CHEUNGKAMING/p/5717429.html
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.1Host: www.tfjyzx.comConnection: keep-aliveContent-Length: 48Pragma: no-cacheCache-Control: no-cacheAccept: application/json, text/javascript, */*; q=0.01Origin: http://www.tfjyzx.comX-Requested-With: XMLHttpRequestUser-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.36Content-Type: application/x-www-form-urlencoded; charset=UTF-8Referer: http://www.tfjyzx.com/login.jspAccept-Encoding: gzip, deflateAccept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,ja;q=0.6Cookie: experience=show; JSESSIONID=C63BF22874A3B791F212CFCBAFFAE432";$header = explode("\n", $s);// http://jp2.php.net/manual/en/function.curl-setopt.phpcurl_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"}
php 发送http请求 GET POST
原文地址:https://www.cnblogs.com/mingzhanghui/p/9280705.html