Getting jSON Data with PHP (curl method)

For a customer project, we needed to do a jSON query with PHP. So I searched the web for a tutorial how to do, but I’ve found much about the PHP jSON functions, but not much how to make requests to jSON interfaces with PHP. So I try to explain you how it works.

Sending Data with PHP to a  jSON Script with curl

To get the data we use the PHP curl functions. In our example we have added a authentication with curl, if not needed, just leave the lines, commented with // authentication.


// jSON URL which should be requested
$json_url = ‘’;

$username = ‘your_username’;  // authentication
$password = ‘your_password’;  // authentication

// jSON String for request
$json_string = ‘[your json string here]’;

// Initializing curl
$ch = curl_init( $json_url );

// Configuring curl options
$options = array(
CURLOPT_USERPWD => $username . “:” . $password,  // authentication
CURLOPT_HTTPHEADER => array(‘Content-type: application/json’) ,
CURLOPT_POSTFIELDS => $json_string

// Setting curl options
curl_setopt_array( $ch, $options );

// Getting results
$result = curl_exec($ch); // Getting jSON result string


The result will be a jSON string.

Normally you have to send a jSON string to the jSON URL and you will get a jSON string back. To encode your data, just use the php jSON functions.  Use json_encode to create a json string from your PHP array and use json_decode to transform the jSON string to PHP array.


30 thoughts on “Getting jSON Data with PHP (curl method)

  1. Thanks man, exactly what I needed!

    For anyone else using json_decode after this, be sure to say $array = json_decode($result, true) otherwise it will return an object, and you wont be able to use it like a regular PHP array.

  2. How to read JSON with php from this url:

    • Deepanshu kakkar says:

      Just like :
      $i = 0;
      for($i = 0; $i<count($fileData); $i++)
      echo "”;
      echo “”.$fileData[$i]->NameOfCompany.””;
      echo “”.$fileData[$i]->Address.””;
      echo “”.$fileData[$i]->City.””;
      echo “”.$fileData[$i]->State.””;
      echo “”.$fileData[$i]->ZipCode.””;
      echo “”.$fileData[$i]->Phone.””;
      echo “”.$fileData[$i]->Established.””;
      echo “”.$fileData[$i]->Website.””;
      echo “”.$fileData[$i]->GooglePlacesURL.””;
      echo “”.$fileData[$i]->Category.””;
      echo “”.$fileData[$i]->EmailAddressForListingsandReporting.””;
      echo “”.$fileData[$i]->AbouttheBusiness.””;
      echo “”.$fileData[$i]->FeaturedSpecial.””;

      echo “”;

  3. Profile photo of mahype

    You can’t include a file with a parameter after it in that way. You have to open the file with the full URL like to get your values with such parameters.

    Also you can’t use the include function for it. You can use the fopen function.

    But be sure that url fopen is not blocket on your server.

  4. Sohail Sheikh says:

    Below are Request & Response given in API document.

    Example Request
    curl -X GET
    -H “X-Auth-Token: 6c602fc6b93d0c4b4797d6481d408f17″
    “ ”
    Example Response
    “collection_id”: 123,
    “name”: “The Lewis Carroll Collection”
    “collection_id”: 156,
    “name”: “Classics: Machiavelli”

    When i try its not working for me its give me invalid token or say Service Unavailable.

    Token detail:—
    {“access”:{“token”:{“id”:”1c92bf205279177c4fdece41e440fc48″,”expires”:”Wed, 12 Jun 2013 11:43:03 +0000″}}}

    i will give my code which i used for that.

    $data = array(“X-Auth-Token” => ’1c92bf205279177c4fdece41e440fc48′);
    $data_string = json_encode($data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    ‘Content-Type: application/json’,
    ‘X-Auth-Token: 1c92bf205279177c4fdece41e440fc48′,
    ‘Content-Length: ‘ . strlen($data_string)

    if(curl_exec($ch) === false)
    echo ‘Curl error: ‘ . curl_error($ch);
    echo ‘Operation completed without any errors’;

    $result = curl_exec($ch);

    Please reply me as soon as possible..

  5. Your article is quite good but there is one thing confusing. When i need to send a large json string it is not sending. How can i resolve this problem. Here is the output of json encoded string.

    “date”: “2013-06-19 05:38:00”,
    “encrypted_string”: “7Y4jqgIfhj25ghsF8yJ/qTtzXafTtIlwsz7xWIDVWJGoF22X2JbfSWfQtgmI1dYyyJDgs3nmaWctTEgKW5VmHw==”,
    “id”: 231,
    “source_ref”: “testreference”,
    “status”: “Active”

  6. I used to get the json data using plain php and found it to be really slow, used this method here and yeah the speed up a bit

  7. Ankush Jetly says:

    to handle json/XML post data we need to use read only php:://input stream instead of http_raw_post_data normal post you can not handle!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>