简体   繁体   中英

Using jQuery instead of cURL for REST API calls in PHP

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM