Last active
February 13, 2016 04:20
-
-
Save mathieu-aubin/f1d994642db9219932de to your computer and use it in GitHub Desktop.
Bare Bones PHP cURL Client for API Consumption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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