Skip to content

Instantly share code, notes, and snippets.

@thrbowl
Created April 7, 2016 07:21
Show Gist options
  • Save thrbowl/f21687e52b22bfe305f82a38d5d95e3f to your computer and use it in GitHub Desktop.
Save thrbowl/f21687e52b22bfe305f82a38d5d95e3f to your computer and use it in GitHub Desktop.
<?php
function CallAPI($method, $url, $username, $password, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result);
}
$response = CallAPI("GET", "http://moon.ap.freescale.net/cbf/api/user/get_user_info", "b43258", "cwbb21bkoiu3wjhp3ru18vprkrml0888");
var_dump($response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment