How to Simulate JSon POST Request Using PHP and CURL

Basically, im trying to do a PHP POST request using JSon string as its format and able to consume JSon array string as responses. Dont forget to activate CURL in your php.ini file.


//set POST variables address and json string
$url = 'http://localhost:81/dudu.php';
$fields = array(

//url-ify the data for the JSON POST
$fields_string = json_encode($fields);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);

//execute post
$jsonResult = curl_exec($ch);

//close connection

// [{"name":0,"email":""},{"name":1,"email":""},
// {"name":2,"email":""},{"name":3,"email":""}]
$results = json_decode($jsonResult, true);
foreach ( $results as $result )
    echo "name : {$result['name']} and email {$result['email']} <br />";


This is what my browser will look like after im testing my JSon POST

Have fun with CURL :-)


No Comments

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked

:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)