Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get JSON response using curl

Tags:

json

php

curl

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'

like image 599
Hasan Avatar asked Nov 23 '12 21:11

Hasan


People also ask

Can JSON parse curl?

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.

How do I see response headers in curl?

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.

Can I send JSON in GET request?

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.

How do I send a GET request using curl?

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.


2 Answers

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

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
like image 189
lafor Avatar answered Sep 22 '22 11:09

lafor


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);
like image 27
cezar Avatar answered Sep 23 '22 11:09

cezar