I've been successfully making API calls using cURL in PHP which returns a JSON array on my local machine. For some reason when I push this code to my host (bluehost), I get back nothing. I don't get any errors, just null.
It was suggested that using cURL is old school and not recommended anyway, and that I should just do this in jQuery. I've put my current code below, can anyone please point me in the right direction as to how to accomplish the same thing in jQuery or another method within PHP?
function CallAPI($method, $url, $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));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($curl);
}
I call this function in PHP like this for GET:
$api_querystring = "https://discreet.afty.io/api/swags?type=0&status={$swag_status}";
$response = CallAPI('GET', $api_querystring);
$badges = json_decode($response);
And I get back a JSON array object like:
Array ( [0] => stdClass Object ( [__v] => 21 [_id] => 51ae35bd005f377a06000018 [apns_token] => 8ef99cb6c8fa2928468cfbaa5b1d6d0244e46ad0ca6dd56e476e26edffbb8c59 [badge] => 51b64157a1facb5b2b000018 [device_id] => EB3D1DFF-5557-4EEA-8BE3-E261FBB5C058 [first_name] => Jeff [last_name] => S [metadata] => stdClass Object ( [limit] => Erotic Chat ) [groups] => Array ( [0] => 51ae99e3005f377a06000036 [1] => 51a84dc3a8a3801477000007 [2] => 51ae70085068176b06000023 [3] => 51af69ad5068176b0600002f [4] => 51b2a0af50c8b0507d00000c [5] => 51a84df6a8a3801477000008 [6] => 51ae5c625068176b06000021 [7] => 51a84e91a8a380147700000a [8] => 51b69543a1facb5b2b000020 [9] => 51b695cddaa06e871900001b [10] => 51b695f1daa06e871900001c [11] => 51a84e81a8a3801477000009 ) [connected] => [admin] => [flagged] => [blocked] => [apns_count] => 0 [apns_status] => 0 [_create_date] => 2013-06-04T18:45:17.460Z [_last_modified] => 2013-06-16T06:17:07.516Z ) )
and for POST like this:
$api_querystring = "https://discreet.afty.io/api/admin/users/{$id}";
$response = CallAPI('POST', $api_querystring, $data);
I know this is probably pretty basic stuff; but this is the first time I've been working with API's (which is cool), but I could use some additional guidance.