I am new to the concept and ideas of curl.I want to post the bellow values to a remot server https://example.com/test/token. The values are client_secret=sk_test_dndkFknKdtMLhv0eA8OdcgW1 , code=GH_Dgl12 , grant_type=dsfgsdK. How can i do this?
2 Answers
Simple example of curl post with your params:
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"client_secret=sk_test_dndkFknKdtMLhv0eA8OdcgW1, code=GH_Dgl12, grant_type=dsfgsdK");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") { ... } else { ... }
?>
The option CURLOPT_RETURNTRANSFER will let curl_exec() return the response so that you can catch it as well.
$url = 'http://domain.com/get-post.php';
$fields = array(
'client_secret' => urlencode('sk_test_dndkFknKdtMLhv0eA8OdcgW1'),
'code' => urlencode('GH_Dgl12'),
'grant_type' => urlencode('dsfgsdK'),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//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($curl, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
-
thanks alot shaddy..can i catch the response from that server? Commented Mar 8, 2013 at 5:10
-
Yes you can just set curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); just before calling curl_exec. Then the response will be assigned to $result :)– Sh1d0wCommented Mar 8, 2013 at 5:21