RPC SOAP REST API API HTTP JSON XML PHP PHP PHP PHP PHP HTTP request/response cycle HTTP HTTP verbs headers Cookies JSON XML PHP RPC SOAP RESTful HTTP API API 前言 vii
第一章 HTTP HTTP HyperText Transfer Protocol web HTTP request response HTTP URL verb headers status code body HTTP [page-1] server client http://lornajane.net 1-1 HTTP http://lornajane.net WordPress
2 HTTP [page-2] GET / HTTP/1.1 Host: www.lornajane.net Connection: keep-alive Cache-Control: no-cache Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/ *;q=0.8 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)... Accept-Encoding: gzip, deflate, sdch Accept-Language: en-gb,en-us;q=0.8,en;q=0.6 GET HTTP/1.1 200 OK Server: Apache/2.4.7 (Ubuntu) X-Powered-By: PHP/5.5.9-1ubuntu4.6 X-Pingback: http://www.lornajane.net/xmlrpc.php Expires: Wed, 11 Jan 1984 05:00:00 GMT Cache-Control: no-cache, must-revalidate, max-age=0 Content-Encoding: gzip Content-Type: text/html; charset=utf-8 Content-Length: 8806 Date: Tue, 15 Sep 2015 08:43:54 GMT X-Varnish: 612483212 Age: 0 Via: 1.1 varnish <!DOCTYPE html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <meta name="bitly-verification" content="ff69fb2e45ef"/> <title>home - LornaJaneLornaJane Lorna Jane Mitchell's Website</title> <link rel="shortcut icon" href="http://www.lornajane.net/wp-content/themes/lj/ images/favicon.ico">... HTTP
HTTP 3 1-1 lornajane.net HTTP Google kittens [page-3] 1. http://www.google.com Location 301 http://www.google.co.uk 2. redirect http://www.google.co.uk 3. https://www.google.co.uk/search?q=kittens
4 HTTP URL 中問號 (?) 之後的部分稱為 查詢字串 (query string), 這是向特定 URL 或端點 (endpoint) 傳遞附加資料的一種方法 assets asynchronously JavaScript API [page-4] PHP PHP API PHP PHP API PHP PHP API API PHP API PHP HTTP tweets 1-2 當你和一個既提供 API 又呼叫 API 的應用程式打交道時, 請注意變數的命名, 建議你加入 request 關鍵字以避免混淆
HTTP 5 1-2 API HTTP HTTP HTTP [page-5] command-line tool PHP 10 debugging HTTP http://requestb.in API request bin URL HTTP API http://example.com http://example.net http://example.org Internet Assigned Numbers Authority http://www.iana. org/domains /special HTTPResponder http://httpresponder.com GitHub
6 HTTP httpbin.org http://requestb.in http://requestb.in/ example HTTP curl http://curl.haxx.se curl PHP curl extension 12 PHP HTTP [page-6] curl curl http://requestb.in/example API Web GET POST GET POST HTTP -X switch POST curl -X POST http://requestb.in/example GET POST -X -v STDERR STDOUT $ curl -v -X POST http://requestb.in/example -d name="lorna" -d email="lorna@example.com" -d message="this HTTP stuff is rather excellent" * Hostname was NOT found in DNS cache * Trying 54.197.228.184... * Connected to requestb.in (54.197.228.184) port 80 (#0)
HTTP 7 > POST /example HTTP/1.1 > User-Agent: curl/7.38.0 > Host: requestb.in > Accept: */* > Content-Length: 78 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 78 out of 78 bytes < HTTP/1.1 200 OK < Connection: keep-alive * Server gunicorn/19.3.0 is not blacklisted[page-7] < Server: gunicorn/19.3.0 < Date: Tue, 07 Jul 2015 14:49:57 GMT < Content-Type: text/html; charset=utf-8 < Content-Length: 2 < Sponsored-By: https://www.runscope.com < Via: 1.1 vegur < * Connection #0 to host requestb.in left intact curl curl less grep curl terminal progress output bar -s curl -s -v less pager curl -s -v http://requestb.in/example 2>&1 less 2>&1 STDERR STDOUT less STDOUT API curl -d field POST curl curl -X POST http://requestb.in/example -d name="lorna" -d email="lorna@example.com" -d message="this HTTP stuff is rather excellent"
8 HTTP API POST JSON XML curl -d curl name=lorna&email=lorna@example.com @ -d curl curl -X POST http://requestb.in/example -d @data.txt HTTP curl -H Accept HTML [page-8] curl -H "Accept: text/html" http://requestb.in/example curl Cookie 4 Cookie Cookie Cookie Cookie curl Cookie Cookie cookie jar HTTP Cookie curl -c cookiejar.txt http://requestb.in/example cookiejar.txt Cookie cookie jar -b Cookie curl -b cookiejar.txt http://requestb.in/example Cookie -b -c cookiejar Cookie
HTTP 9 curl HTTPie http://httpie.org curl curl [page-9] 1-3 HTTPie 1-3 curl HTTPie GET --print -p HTTPie H B h b hb curl b http -p b http://requestb.in/example HTTPie URL POST
10 HTTP $ http -p bhbh -f http://requestb.in/example name=lorna email=lorna@example.com message="this HTTP stuff is rather excellent" POST /example HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate Connection: keep-alive Content-Length: 80 Content-Type: application/x-www-form-urlencoded; charset=utf-8 Host: requestb.in User-Agent: HTTPie/0.8.0 name=lorna&email=lorna%40example.com&message=this+http+stuff+is+rather+excellent HTTP/1.1 200 OK[page-10] Connection: keep-alive Content-Length: 2 Content-Type: text/html; charset=utf-8 Date: Tue, 07 Jul 2015 14:46:28 GMT Server: gunicorn/19.3.0 Sponsored-By: https://www.runscope.com Via: 1.1 vegur ok HTTPie : Accept $ http -p H -f http://requestb.in/example Accept:text/html GET /149njzd1 HTTP/1.1 Accept: text/html Accept-Encoding: gzip, deflate Connection: keep-alive Content-Type: application/x-www-form-urlencoded; charset=utf-8 Host: requestb.in User-Agent: HTTPie/0.8.0 curl HTTPie HTTP Chrome Firefox Opera Safari IE HTTP
HTTP 11 API Firefox Developer Toolbar plugin FireBug http://getfirebug.com LiveHTTPHeaders http://livehttpheaders.mozdev.org 1-4 1-4 LiveHTTPHeaders HTTP Cookie Chrome Edit This Cookie Cookie Cookie Cookie [page-11] authentication curl
12 HTTP Chrome ModHeader 1-5 1-5 Chrome ModHeader PHP HTTP PHP HTTP PHP API PHP curl PHP PHP stream-handling [page-12] Guzzle PHP curl 6 HTTP PHP curl wrapper PHP GET <?php
HTTP 13 $url = "http://www.lornajane.net/"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); URL GET curl_setopt() curl http://php.net CURLOPT_RETURNTRANSFER curl HTTP flag [page-13] HTTP POST JSON Content-Type <?php $url = "http://requestb.in/example"; $data = ["name" => "Lorna", "email" => "lorna@example.com"]; $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json'] ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); curl_setopt() POST CURLOPT_POST 1 CURLOPT_ POSTFIELDS Content-Type 3 PHP curl interface PHP HTTP PHP allow_url_fopen
14 HTTP PHP http://bit.ly/php-allow_url_fopen file_get_contents() GET <?php $result = file_get_contents("http://www.lornajane.net/"); PHP HTTP FTP SSL GET JSON POST [page-14] <?php $url = "http://requestb.in/example"; $data = ["name" => "Lorna", "email" => "lorna@example.com"]; $context = stream_context_create[[ 'http' => [ 'method' => 'POST', 'header' => ('Accept: application/json', 'Content-Type: application/json'), 'content' => json_encode($data) ] ]]; $result = file_get_contents($url, false, $context); context PHP PHP HTTP Guzzle http://guzzlephp.org PHP library HTTP Composer http://getcomposer.org Composer GitHub 6 Guzzle POST Guzzle <?php require "vendor/autoload.php";
HTTP 15 $url = "http://requestb.in/example"; $data = ["name" => "Lorna", "email" => "lorna@example.com"]; $client = new \GuzzleHttp\Client(); $result = $client->post($url, ["json" => $data]); echo $result->getbody(); Guzzle http://docs.guzzlephp.org Composer autoloader Guzzle URL Guzzle client POST json Guzzle JSON http://requestb.in [page-15] HTTP PHP PHP framework Guzzle