Make HTTP Requests Using Curl and Decoding JSON Responses in PHP
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.
- <?php
- $requesturl=‘http://api.soundcloud.com/tracks/54518918.json?client_id=d70658d99cfe7bf44a398e3f8b1785ed’;
- $ch=curl_init($requesturl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $cexecute=curl_exec($ch);
- curl_close($ch);
- $result = json_decode($cexecute,true);
- echo “Id: “.$result['id'];
- echo “<br>Title: “.$result['title'];
- echo “<br>Description: “.$result['description'];
- ?>
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
Follow @phpzag

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