Skip to content

Instantly share code, notes, and snippets.

Last active February 4, 2019 16:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Sending URL requests (POST/GET) in PHP using curl
$url = '';
$fields = array(
'key1'=>urlencode('value 1'),
'key2'=>urlencode('value 2'),
'key3'=>urlencode('value 3')
//url-ify the data for the POST
$fields_string = "";
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($ch,CURLOPT_RETURNTRANSFER, 1); // stop return output
//execute post
$result = curl_exec($ch);
//close connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment