February 16th, 2013, 07:13 AM
-
How to do a PHP HTTP GET request without using libraries.
Hi,
I am looking for a way to make an HTTP GET request using PHP without using an external library.
So something like:
PHP Code:
$response = file_get_contents("https://api.foursquare.com/v2/venues/search?ll=40.7,-74&client_id=<CLIENT_ID>&client_secret=<CLIENT_SECRET>&v=YYYYMMDD");
echo $response;
Most solutions that I find on the web are using CURL or file_get_contents. I don't want to use curl because i'd like to do it without a library (for now). The example above doesn't work as I get the following error:
"failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request"
How would I do this HTTP GET request the right way?
Hope you can help me out.
Thanks in advance!
PS. What I specifically want is to do a search request to foursquare and get a JSON response back that will be stored in the $response variable.
Last edited by Stefan1; February 16th, 2013 at 11:14 AM.
February 16th, 2013, 08:55 AM
-
Well, my first question is, is the address provided in ur example correct?
It seems anything of such I've typed after the .com/ pulls up a "Page cannot be found" via "400 Bad Request" as you are also receiving via script.
And "https://api.foursquare.com/" redirects to "https://developer.foursquare.com/"
I have a possible few examples for you, but am not familiar with HTML GET items, so hoping for an example of what the response would be.
February 16th, 2013, 11:13 AM
-
Thanks for the reply.
The URL is not correct in the sense that I did not provide a proper client ID and client secret as url parameter.
However, if you replace the <CLIENT_ID> and <CLIENT_SECRET> with anything, for example "1", and then copy that url into the browser, there is actually an output in like this:
{"meta":{"code":400,"errorType":"invalid_auth","errorDetail":"Missing access credentials. See https:\/\/developer.foursquare.com\/docs\/oauth.html for details."},"response":{}}
So this is what I should expect as a response in the PHP script right?
February 16th, 2013, 03:13 PM
-
I found out that file get contents actually does work when I use http instead of https.
However foursquare requires https, because I get the following message back:
{"meta":{"code":404,"errorType":"other","errorDetail":"api v2 requests must be sent over https"},"response":{}}
I did some searching and I found people say that for https "allow_url_include" should be on in the PHP settings, which is not on, on my server.
Any ideas how to solve this? Would using cURL solve this problem?
Thanks in advance!
Stefan
February 16th, 2013, 06:11 PM
-
The setting you need is allow_url_fopen which is typically enabled. allow_url_include is for something else and should be turned off.
The request itself is probably valid so I think the service is rejecting your request for some other reason. Are you sure you're using the right credentials? Are you in some kind of sandbox mode?