# PHP - Data Explorer API Example

**URL:** https://meta.discourse.org/t/php-data-explorer-api-example/348668
**Category:** Data & reporting
**Tags:** rest-api
**Created:** [January 23, 2025, 7:04pm UTC](https://meta.discourse.org/t/php-data-explorer-api-example/348668 "2025-01-23T19:04:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![LotusJeff](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lotusjeff/32/477888_2.png) [@LotusJeff](https://meta.discourse.org/u/LotusJeff)
#### Post date: [January 23, 2025, 7:04pm UTC](https://meta.discourse.org/t/php-data-explorer-api-example/348668/1 "2025-01-23T19:04:19Z")

</div>

I am working on pulling data out of Discourse using PHP and the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) API. Here is the skeleton of my PHP code for others as a starting point.

#### PHP Code

```php
<?php
// Set API key
$api_key = '<your key goes here>';
//
// 1. $url should be set to your Discourse forum URL
// 2. Obtain the query # you want to run. Update the url for that query
// i.e. query #5 => .../queries/5/run
// i.e. query #47 => .../queries/47/run
$url = 'https://discourse.com/admin/plugins/explorer/queries/7/run';
//
// Update your parameters to pass to the query.
// Example for one parameter - hard coded
// $data = 'params={"user":"fred_smith"}';   
// Example for one parameter - passing a variable
// $data = 'params={"user":"'.$user.'"}';   
// Example for two or more paramters - passing a variable
$data = 'params={"user":"'.$user.'","term":"'.$term.'"}';   
//
// Curl Commands
//
$headers = array("Content-Type: multipart/form-data;","Api-Key: $api_key","Api-Username: system",);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

$result = curl_exec( $ch );
if ( curl_errno( $ch ) !== 0 ) {
   // Handle error, call curl_close( $ch ) and return.
}
curl_close( $ch );
//
// Convert curl result to a php array to programatically work with
$result_array = json_decode( $result,true);

print($result_array);

?>

```

#### [Data Explorer](https://meta.discourse.org/t/32566?silent=true) Query

In the example of the code using two passed variables, my data explore code looked like this:

```plaintext
--[params]
-- string :term
-- string :user

SELECT users.username, posts.id, uploads.url, topics.title
     FROM upload_references
     JOIN uploads ON uploads.id = upload_references.upload_id
     JOIN posts ON posts.id = upload_references.target_id
     JOIN topics ON topics.id = posts.topic_id
     JOIN users ON posts.user_id = users.id
    WHERE (posts.cooked like '%' || :term || '%' 
       OR topics.title like '%' || :term || '%') 
      AND users.username_lower like '%' || :user || '%'
      AND topics.archetype = 'regular'
 ORDER BY uploads.created_at DESC
 limit 10

```

I hope this helps someone jumpstart a project.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [January 23, 2025, 7:48pm UTC](https://meta.discourse.org/t/php-data-explorer-api-example/348668/2 "2025-01-23T19:48:51Z")

</div>

I’d trim most of the comments to make it easier to read 🙂  
Some comments are unnecessary, like:

```plaintext
// Obtain result
//
$result = curl_exec( $ch );

```

or

```plaintext
// close curl session
curl_close( $ch );

```

Also, know you can set the syntax highlighting by appending the language name to the first line of backticks:

````plaintext
```php
$headers = array("Content-Type: multipart/form-data;","Api-Key: $api_key","Api-Username: system",);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
```

````

Renders as

```php
$headers = array("Content-Type: multipart/form-data;","Api-Key: $api_key","Api-Username: system",);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

```

---

<div class="post-metadata">

### Author: ![LotusJeff](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lotusjeff/32/477888_2.png) [@LotusJeff](https://meta.discourse.org/u/LotusJeff)
#### Post date: [January 23, 2025, 8:09pm UTC](https://meta.discourse.org/t/php-data-explorer-api-example/348668/3 "2025-01-23T20:09:03Z")

</div>

Thanks for the feedback. I updated the initial posts.
