Last active
May 20, 2016 00:14
-
-
Save nickmalcolm/a9e2899ca869e67dd8e4d1ed934d0304 to your computer and use it in GitHub Desktop.
Use ThisData's API using PHP curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$user = array("id" => "1234", "email" => "[email protected]"); | |
$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