Reputation: 516
I'm accessing one of the API for my client that is opening well when I request any method by typing it in the url box. Here is the request/response snapshot from my Chrome browser.
Request URL:http://example.com/RestAPI/api/holding_info.action?deviceId=1&holdingId=23
Request Method:GET
Status Code:200 OK
Request Headers
---------------
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Host:risto.enon.biz
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Query String Parametersview sourceview URL encoded
deviceId:1
holdingId:23
Response Headers
----------------
Connection:Keep-Alive
Content-disposition:filename="response.json"
Content-Type:application/json
Date:Thu, 20 Mar 2014 06:13:52 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.22 (Ubuntu)
Transfer-Encoding:chunked
On IE however same request serves a file to download which I think is a strange behaviour. The question is how to consume such an API in PHP. In PHP what ever method I'm using i.e. curl, file_get_contents() and using external libs, like Guzzle, httpful etc, the response I'm getting is really strange. see the picture attached.
In picture you can see that I've var_dump()ed the responded text and it contains some strange characters in between each valid character. Due to these strange characters I'm not able to parse this as a json.
Why this API is so different? I have read somewhere that some APIs require client to keep session open to get the stream of data but I'm not aware much of it how to handle that. Also why then chrome is getting it right? I've a strong guess the problem is due to that Transfer-Encoding:chunked
in the response header. Please help me consuming such an API in PHP.
Upvotes: 1
Views: 850