| Response headers can contain valuable information and may help to keep your API responses simpler by separating the actual response data from accessory metadata. |
| For instance, when querying the API for a list of posts, the response body includes just the content but there are also some other valualbe information sent as headers: |
| The PHP code for the CURL request would look something like this: |
$agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 X-Client-Data: CIa2yQEIpLbJAQjBtskBCKmdygEIqKPKARiSo8oB"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_FILETIME, true); curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_USERAGENT, $agent); $header = curl_exec($curl); $info = curl_getinfo($curl); curl_close($curl); |
| Which will output as below: |
HTTP/1.1 200 OK
Date: Fri, 22 Jun 2018 06:47:27 GMT
Server: Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/5.6.35
Last-Modified: Sun, 11 Feb 2018 16:39:41 GMT
ETag: "5ae6f-564f2687e28f8"
Accept-Ranges: bytes
Content-Length: 372335
Content-Type: application/pdf
Array
(
[url] => http://localhost/text-finder/download.pdf
[content_type] => application/pdf
[http_code] => 200
[header_size] => 265
[request_size] => 258
[filetime] => 1518367181
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.016
[namelookup_time] => 0.016
[connect_time] => 0.016
[pretransfer_time] => 0.016
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 372335
[upload_content_length] => -1
[starttransfer_time] => 0.016
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => ::1
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => ::1
[local_port] => 51877
)
HTTP/1.1 200 OK
Date: Fri, 22 Jun 2018 06:48:11 GMT
Server: Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/5.6.35
Last-Modified: Fri, 22 Jun 2018 04:18:28 GMT
ETag: "92-56f3352eebe85"
Accept-Ranges: bytes
Content-Length: 146
Content-Type: text/html
Array
(
[url] => http://localhost/text-finder/test2.html
[content_type] => text/html
[http_code] => 200
[header_size] => 253
[request_size] => 256
[filetime] => 1529641108
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.015
[namelookup_time] => 1.0E-6
[connect_time] => 0.015
[pretransfer_time] => 0.015
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 146
[upload_content_length] => -1
[starttransfer_time] => 0.015
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => ::1
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => ::1
[local_port] => 51944
)
|
Friday, June 22, 2018
Header only retrieval in php via curl | PHP CURL get content type from URL | PHP CURL retrieve headers information from URL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment