* CURL

  http://www.php.net/manual/en/book.curl.php

  http://jp2.php.net/manual/en/function.curl-setopt.php

 

RESTful

+ -------------------------------------+

| GET POST PUT DELETE    |

+-----------------------------------  +

DELETE:

关键的配置项:

CURLOPT_CUSTOMREQUEST => 'DELETE',

<?php

$ch = curl_init();

$token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTEyNTU0MjcsImlkIjowLCJuYmYiOjE1NTEyNTU0MjcsInVzZXJuYW1lIjoiYWRtaW4ifQ.NFlKmsM5ceJhJRirr9b3jYRjg1XKFcoNrb_wTqrJ9wE';

$headers = array(
    "X-Requested-With: XMLHttpRequest",
    "Content-Type: application/x-www-form-urlencoded",
    "Authorization: Bearer ".$token,
);

$postdata = "";
$userid = 8;

curl_setopt_array($ch, [
    CURLOPT_URL => 'http://192.168.4.157:8060/v1/user/'.$userid,
    CURLOPT_HEADER => 1,        // 返回结果带http header?
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_POST => 1,      // type: "POST" ?
    CURLOPT_CUSTOMREQUEST => 'DELETE',  // "PUT", "DELETE", ...
    CURLOPT_BINARYTRANSFER => 1,
    CURLOPT_HTTPHEADER => $headers,   // request header
    CURLOPT_POSTFIELDS => $postdata   // request data
]);

$data = curl_exec($ch);
curl_close($ch);

echo $data.PHP_EOL;

 

  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