5

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'

2 Answers 2

6

The problem is http*s*, try adding these:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah! I tried echo curl_error($ch) just before your answer, and output was about SSL verification and I was going to search for it. Answer is faster than the question ^^
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); 

3 Comments

""Use of undefined constant CURLOPT_GET - assumed 'CURLOPT_GET'""
Yes, sorry didn't use GET method in a while, check this codular.com/curl-with-php
You may also need to set the header to json curl_setopt(CURLOPT_HTTPHEADER,'Content-type: application/json');

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.