curl post php example with json response

CURL:

CURL is basically to hit the url through GET/POST request and to receive the response. Response can be anything. [JSON/XML/plain string]

 

When I was searching for the sample curl post php example program in internet, I could not find the proper executing programs.

So I wrote my own program and sharing here to help our blog users.

 

In the below sample program, you can directly give the URL and post parameter names and values and proceed your business logics.

 

Genius API :

We have used GeniusAPI to retrieve the song lyrics information’s based on the artist name. This request returns JSON response.

 

 

curl post php example with json response:

[php]

<?php
$ch = curl_init();
// Give your post request details [name and value]
$postParamsArray = array( ‘name’=>’AR Rahman’, ‘genre’=>’rap’);
$postParams = "";
foreach($postParamsArray as $postParamName=>$postParamValue) {
$postParams .= $postParamName . "=" . $postParamValue;
$postParams .= "&";
}
// Give your URL here
$reqURL = "http://genius-api.com/api/artistInfo";
curl_setopt($ch,CURLOPT_URL,$reqURL);
// 1 – post | 0 – get
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postParams);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
// to print the response
//print "Response is:" . $response;
$json = json_decode($response, true);
// printing the JSON response
print_r($json);
curl_close ($ch);
?>

[/php]

 

Output:

[plain]

<span style="font-size: 0.95em; line-height: 1.6em;">Array ( [name] => A. R. Rahman [link] => http://rapgenius.com/artists/A.-R. Rahman [popularSongs] => Array ( [0] => Array ( [name] => Jai Ho by Sukhvinder Singh, Mahalaxmi Iyer and Vijay Prakash (Ft. Tanvi Shah) prod. by Gulzar [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/Sukhvinder-singh-mahalaxmi-iyer-and-vijay-prakash-jai-ho-lyrics ) [1] => Array ( [name] => Million Dollar Dream by Iggy Azalea (Ft. Sukhwinder Singh) [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/Iggy-azalea-million-dollar-dream-lyrics ) [2] => Array ( [name] => Nenje Ezhu songs [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-nenje-ezhu-songs-lyrics ) [3] => Array ( [name] => A.r.rahman so [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-arrahman-so-lyrics ) [4] => Array ( [name] => Maahi Ve prod. by Imtiaz Ali & Sajid Nadiadwala [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-maahi-ve-lyrics ) [5] => Array ( [name] => Ringa Ringa (Ft. Alka Yagnik & Ila Arun) [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-ringa-ringa-lyrics ) ) [songs] => Array ( [0] => Array ( [name] => Jai Ho by Sukhvinder Singh, Mahalaxmi Iyer and Vijay Prakash (Ft. Tanvi Shah) prod. by Gulzar [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/Sukhvinder-singh-mahalaxmi-iyer-and-vijay-prakash-jai-ho-lyrics ) [1] => Array ( [name] => Million Dollar Dream by Iggy Azalea (Ft. Sukhwinder Singh) [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/Iggy-azalea-million-dollar-dream-lyrics ) [2] => Array ( [name] => Nenje Ezhu songs [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-nenje-ezhu-songs-lyrics ) [3] => Array ( [name] => A.r.rahman so [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-arrahman-so-lyrics ) [4] => Array ( [name] => Maahi Ve prod. by Imtiaz Ali & Sajid Nadiadwala [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-maahi-ve-lyrics ) [5] => Array ( [name] => Ringa Ringa (Ft. Alka Yagnik & Ila Arun) [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-ringa-ringa-lyrics ) [6] => Array ( [name] => A.r.rahman so [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-arrahman-so-lyrics ) [7] => Array ( [name] => Jai Ho by Sukhvinder Singh, Mahalaxmi Iyer and Vijay Prakash (Ft. Tanvi Shah) prod. by Gulzar [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/Sukhvinder-singh-mahalaxmi-iyer-and-vijay-prakash-jai-ho-lyrics ) [8] => Array ( [name] => Million Dollar Dream by Iggy Azalea (Ft. Sukhwinder Singh) [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/Iggy-azalea-million-dollar-dream-lyrics ) [9] => Array ( [name] => If I Rise by Dido [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/Dido-if-i-rise-lyrics ) [10] => Array ( [name] => Maahi Ve prod. by Imtiaz Ali & Sajid Nadiadwala [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-maahi-ve-lyrics ) [11] => Array ( [name] => Nenje Ezhu songs [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-nenje-ezhu-songs-lyrics ) [12] => Array ( [name] => Ringa Ringa (Ft. Alka Yagnik & Ila Arun) [artists] => http://rapgenius.com/artists/A.-R. Rahman [link] => http://rapgenius.comhttp://genius.com/A-r-rahman-ringa-ringa-lyrics ) ) ) </span>

<span style="font-size: 0.95em; line-height: 1.6em;">
[/plain]

 

 

 

CURL in Windows:

 

Downloading Curl for Windows:

Curl for Windows

Installing CURL in windows:

Once you downloaded the msi (or) exe file from the above link double click and install it.

 

Environment Variable Path Setting:

Start Program -> My computer (Right click) -> Properties -> Advanced system settings

 

curl php post example

 

env_variables

 

 

Enter the CURL installed path,

curl_installed_path

 

 

Giving the CURL_PATH in the path,

setting curl path in path

 

 

Open the command prompt,

cmd

CURL Request:

[plain]

curl -d "name=jai ho" -d "genre=rap" "http://genius-api.com/api/songInfo"

[/plain]
cmd curl in windows

 

 

 

 

 

 

Leave a Reply