Skip to content

Instantly share code, notes, and snippets.

@nickmalcolm
Last active May 20, 2016 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickmalcolm/a9e2899ca869e67dd8e4d1ed934d0304 to your computer and use it in GitHub Desktop.
Save nickmalcolm/a9e2899ca869e67dd8e4d1ed934d0304 to your computer and use it in GitHub Desktop.
Use ThisData's API using PHP curl
<?php
$user = array("id" => "1234", "email" => "me@example.com");
$data = array("verb" => "log-in", "ip" => "1.2.3.4", "user_agent" => "Chrome", "user" => $user);
$data_string = json_encode($data);
$url = 'http://api.thisdata.dev:3000/v1/events.json';
$api_key = 'ABC123'; // Use your real API key here
$ch = curl_init($url. "?api_key=". $api_key);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
if ($output === false || $info['http_code'] != 200) {
$error = "No cURL data returned for $url [". $info['http_code']. "]";
$error .= "\n". $output;
if (curl_error($ch)) {
$error .= "\n". curl_error($ch);
}
echo $error;
}
curl_close($ch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment