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.


What Do You Think?

Jump To Comments

What Do You Think?

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.


    • function UrlOpener($url){
      global $output;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      $output = curl_exec($ch);
      echo $output;

      You can use this function to get content use curl url php. Sample here:

      Put URL on form and get content


  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.

    • 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 “”;

  2. 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..

  3. Raheel Shan says:

    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”

  4. logovectors says:

    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

  5. 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!!!

  6. Hi,

    $ch = curl_init($url); // URL returns a json
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $manage = json_decode($data);
    echo $manage;

    – Recoverable fatal error: Object of class stdClass could not be converted to string in…

    How could I obtain an array object?

    Nice tutorial and thanks for your time!
    Best regards.

  7. I try to get api game list from igdb but I got blank page with code above..
    How I can get username auth and pass auth? Thanks!