3

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.

Jeff Solomon
  • 459
  • 6
  • 21
  • 2
    jQuery is a javascript library. If you want to use jQuery for API calls, you will be doing the requests on the client side (in the browser) using javascript. This will likely change the way your web app is structured – compid Jun 16 '13 at 17:41
  • alternatively you can try this: http://stackoverflow.com/a/9802864/1407034 It is an alternative way to make a GET request in PHP – compid Jun 16 '13 at 17:53
  • Is it just me, or does anyone else also feel `cURL` is **not** old-school ? – gkalpak Jun 17 '13 at 06:17

1 Answers1

0

First of all, jQuery is a javascript library. Javascript executes in a client's browser, and not on the server. This means using javascript could expose more application logic, but at the same time enhance user experience/responsiveness because you can use javascript to directly access the DOM and modify the webpage without additional server requests.

To make a request in javascript, we do it via AJAX, which is a way to asynchronously make a request to a server. To fully take advantage of this, you will need to learn javascript, jQuery, as well as how to perform AJAX requests in jQuery and how to manipulate the DOM (the webpage) with jQuery.

In the most boiled down and isolated form, here is the javascript code (using the jQuery library) that would make a request:

$.ajax({
  url: "https://discreet.afty.io/api/swags",
  type: "get",
  data: {
    type: 0,
    status: somevar
  },
  success: function(data) {
    // do something with returned JSON
  }
});

I'll keep this as a community wiki so that other users can add resource for getting started with Javascript/jQuery/AJAX.

compid
  • 1,313
  • 9
  • 15
  • Thanks; I've done some AJAX requests in the past to CRUD w/ MySQL, formatting looks pretty similar. I will try to play with this. Thanks. – Jeff Solomon Jun 16 '13 at 21:09