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.
<?php //set POST variables address and json string $url = 'http://localhost:81/dudu.php'; $fields = array( 'userName'=>'yyy@xxx.com', 'password'=>'yyyy', 'emailProvider'=>'xxxx' ); //{"userName":"yyy@xxx.com","password":"yyyy","emailProvider":"xxxx"} //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_URL,$url); curl_setopt($ch,CURLOPT_POST,count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); //execute post $jsonResult = curl_exec($ch); //close connection curl_close($ch); // [{"name":0,"email":"yyy@xxx.com"},{"name":1,"email":"yyy@xxx.com"}, // {"name":2,"email":"yyy@xxx.com"},{"name":3,"email":"yyy@xxx.com"}] $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