Make HTTP Requests Using Curl and Decoding JSON Responses in PHP

August 1st, 2012 by Laeeq | 5 comments

PHP has Curl library that let’s you communicate with other servers. The Curl libaray is enabled by installing cUrl extension in PHP.INI. You just need to uncomment “extension=php_curl.dll” in PHP.INI file to install Curl library.

As a php developer, sometimes we need to get data from other server or need to make request to other server like payment gateways integration, fetching catalog etc. By using Curl library, You can make http request to a server and get the required data.  you can also decode the JSON result with json_decode() function.

Here in this post, We will make a Curl request to soundcloud api to get the details of a track. The result will be a list of track details in json data format. The json_decode() function will then convert the json object into an array and will finally print out the details.

  1. <?php
  2. $requesturl=‘http://api.soundcloud.com/tracks/54518918.json?client_id=d70658d99cfe7bf44a398e3f8b1785ed’;
  3. $ch=curl_init($requesturl);
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  5. $cexecute=curl_exec($ch);
  6. curl_close($ch);
  7. $result = json_decode($cexecute,true);
  8. echo “Id: “.$result['id'];
  9. echo “<br>Title: “.$result['title'];
  10. echo “<br>Description: “.$result['description'];
  11. ?>

Output:

Id: 54518918

Title: there might be coffee

Description: work in progress, still needs to be mixed n mastered. ill get around to it eventually

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. August 1st, 2012 at 18:41 | #1

    I am not much good in php coding but I can read some of its syntax and your work is very interesting. I hope this could be useful to all designers and coders who look for solutions on HTTP. great work

  2. August 2nd, 2012 at 23:41 | #2

    Wonderful goods from you, man. I’ve understand your stuff previous to and you’re just extremely excellent. I really like what you’ve acquired here, certainly like what you are stating and the way in which you say it. You make it enjoyable and you still care for to keep it wise. I cant wait to read much more from you. This is really a great web site.

  3. August 13th, 2012 at 19:44 | #3

    Some of the PHP code will not work without the help of the php library. Your work on curl is very much appreciated and in good functions. I love this stuff although sometimes a bit confusing on some parts but over all this one works perfectly. Thanks.

  4. August 19th, 2012 at 01:45 | #4

    I simply want to tell you that I’m all new to weblog and absolutely savored you’re website. More than likely I’m want to bookmark your site . You certainly come with really good stories. With thanks for sharing with us your web page.

  5. August 20th, 2012 at 17:39 | #5

    Hello there, I discovered your website via Google while looking for a comparable subject, your website came up, it appears to be like great. I have added to my favourites|added to my bookmarks.

  1. No trackbacks yet.