Skip to content

Calling Plumber APIs from PHP#

Requirements#

To call APIs hosted in RStudio Connect from PHP, you'll need:

  1. URL for the API endpoint hosted on RStudio Connect
  2. API key (if your API requires authorization)

PHP example#

You can use the cURL extension in PHP to call Plumber APIs from PHP applications:

<!DOCTYPE html>
<html>
<body>

<?php

$connect_api_url = "https://connect.yourcompany.com/rest-api/route";
$connect_api_key = "YfB5XBRB7slkkBSEi5qr93mWJvbpXQQy";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $connect_api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Authorization: Key " . $connect_api_key
));

$result = curl_exec($curl);
curl_close($curl);
echo $result;

?>

</body>
</html>

You can replace the values of $connect_api_url and $connect_api_key with your API URL and API key from RStudio Connect.

Scope#

The code examples assume that you are calling a published API in RStudio Connect that is:

  • Hosted on a secure server with TLS/SSL at an HTTPS endpoint
  • Using an API key to make an authorized call to an API
  • Making an HTTP GET request to the API

If your use case is different, then you can modify the example code accordingly.