Reputation: 109
For simplicities sake I have cut off most of this response string
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 06 Oct 2016 17:16:58 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Rate-Limit-Limit: 300
X-Rate-Limit-Remaining: 297
X-Rate-Limit-Reset: 24
Strict-Transport-Security: max-age=86400 { "status" : 200, "requestId" : "0290bc44-ee91-44b5-8a4e-d2abde77dda2", "likelihood" : 0.89, "photos" : [ { "type" : "facebook", "typeId" : "facebook", "typeName" : "Facebook", "url" : "https://d2ojpxxtu63wzl.cloudfront.net/static/354316d570e9a875994ebace97f5c499_aadb87c0d5bfdca27ec6d9de1467b45856e2d1e9d5aba04a50cfaa76c0887dd9", "isPrimary" : true }, { "type" : "linkedin", "typeId" : "linkedin", "typeName" : "LinkedIn", "url" : "https://d2ojpxxtu63wzl.cloudfront.net/static/fbb7ab1a477ac0721fa1b6343e65cda8_69b2ec8691007604f741862a51146ab259c689db392a21ab3df697248df68d17" }, { "type" : "google", "typeId" : "google", "typeName" : "GooglePlus", "url" : "https://d2ojpxxtu63wzl.cloudfront.net/static/f08e1cdd3673736fe94a131eb02d5cf2_1d48402d2de6af8479415653cb1f2eb52a715650862e50a6bcac0a6477e32a07" }, { "type" : "gravatar", "typeId" : "gravatar", "typeName" : "Gravatar", "url" : "https://d2ojpxxtu63wzl.cloudfront.net/static/da16a86493fdb3ab77238be7ac5161e6_33ad1223f2c4813fe954eb33cf2f9f3b2cd597b7331be6b53646dd9dd0e68152" },
I am grabbing the http headers that I need in this request using php. After words I will need to grab everything minus the http headers to return and be used. I have tried using substr()
to cut and use everything after a certain amount of characters (the end of the headers) but has proven untrustworthy as it cuts it off after an exact amount of characters and the HTTP request is subject to change at any time. I expect that regular expressions will be key to this but I would appreciate any helpful ways to go about this. I will be needing to cut the string off right after max-age=86400. Everything after that is the JSON that will need parsed
Upvotes: 0
Views: 59
Reputation: 109
So what is returned as an entire string with no New line escapes. But I did find an alternative solution that might be a little more complex.
preg_match_all('/X-Rate-Limit-.{0,}/',$this->response_json, $out, PREG_PATTERN_ORDER);
preg_match("/\d{1,}/",$out[0][0],$limit); preg_match("/\d{1,}/",$out[0][1],$remaining); preg_match("/\d{1,}/",$out[0][2],$reset); $pos = strpos($this->response_json, '{'); $response = substr($this->response_json, $pos);
Upvotes: 0
Reputation: 2293
You could look for the first occurrence of /r/n/r/n
as that is the delimiter between the header and the body. You could do something like this.
list($header, $body) = explode("\r\n\r\n", $response, 2);
var_dump($header);
var_dump($body);
It looks like your server is returning encoded chunks in the body, so you wont be able to decode the json until you decode the chunks. This all depends on how you are fetching the data in the first place.
Upvotes: 0
Reputation: 424
In PHP, you probably want
$body = file_get_contents('php://input');
Which will retrieve the body of the request.
The request you posted seems to be malformed as there should be some newlines between the headers and that JSON, but it looks like (and is most likely to be) the case that the JSON is being transmitted as the request body.
You can find more information about getting at the body in PHP in this answer: https://stackoverflow.com/a/8945912/376841
Upvotes: 1