Hey there I am started to learn foursquare API, but I am stuck at getting an Access Token. Here is a part from the code I found in SO.
// build url
$url = 'https://foursquare.com/oauth2/access_token';
$url .= '?client_id='.CLIENT_ID;
$url .= '&client_secret='.CLIENT_SECRET;
$url .= '&grant_type=authorization_code';
$url .= '&redirect_uri=**********/callback'; //change to your 4sq callback
$url .= '&code='.$code;
// call to https://foursquare.com/oauth2/access_token with $code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
However this did not work, so I have tried to find error. First echoed $url and manually clicked on that link. It worked, foursquare has returned me an access token in json format. So the problem is in the curl part of the code.
Can you find my error? and more importantly can you suggest me some resources to study on curl?
EDIT: I did a var_dump($result) and the output is 'boolean false'
This option does not make curl actually understand or know about the JSON data it sends, but it makes it easier to send it. curl will not touch or parse the data that it sends, so you need to make sure it is valid JSON yourself. You can use multiple --json options on the same command line.
In default mode, curl doesn't display request or response headers, only displaying the HTML contents. To display both request and response headers, we can use the verbose mode curl -v or curl -verbose . In the resulting output: The lines beginning with > indicate request headers.
To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.
To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option. The target URL is passed as the first command-line option.
The problem is http*s*, try adding these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
If it is a POST a request then this is the proper way to do it :
$body.='client_id='.CLIENT_ID etc.
$c = curl_init ();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With