Skip to content

Instantly share code, notes, and snippets.

@mathieu-aubin
Last active February 13, 2016 04:20
Show Gist options
  • Save mathieu-aubin/f1d994642db9219932de to your computer and use it in GitHub Desktop.
Save mathieu-aubin/f1d994642db9219932de to your computer and use it in GitHub Desktop.
Bare Bones PHP cURL Client for API Consumption
<?php
// Author: https://gitlab.com/u/jordanbsanders
// Taken from https://gitlab.com/snippets/14460
// Will want to determine protocol and domain dynamically.
$api_endpoint = 'http://my.domain.com/api/endpoint';
$post_data = array(
'username' => urlencode('fake_username'),
'password' => urlencode('fake_password')
);
$post_data_string = '';
foreach($post_data as $key => $value)
$post_data_string .= $key . '=' . $value . '&';
rtrim($post_data_string, '&');
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $api_endpoint);
curl_setopt($handle, CURLOPT_POST, count($post_data));
curl_setopt($handle, CURLOPT_POSTFIELDS, $post_data_string);
$result = curl_exec($handle);
curl_close($handle);
echo $result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment