# 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:** 1\
**Showing post:** 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.

---

_[View the full topic](https://meta.discourse.org/t/php-data-explorer-api-example/348668)._
