Solved

PHP curl

  • 9 September 2023
  • 1 reply
  • 274 views

I’m just starting out with Amp and I’m trying to Setup HTTP API using curl.

My problem is that I “have to” convert their example code:

curl -X POST https://api2.amplitude.com/2/httpapi -H 'Content-Type: application/json' -H 'Accept: */*' --data '{ "api_key": "XXXXXXXXXX”, "events": [{ "device_id": "<INSERT DEVICE ID>", "event_type": "Sign up" }] }'

 

To something like this (this is my code so far and it gives me “

Forbidden

You don't have permission to access this resource.” error.

:


 $vars = array("api_key" => "XXXXXXXXXXXXX",
      "events" => [{
        "device_id" => "<INSERT DEVICE ID>",
        "event_type" => "Sign up");

 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api2.amplitude.com/2/httpapi");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);  //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [
    'Content-Type: application/json',
    'Accept: */*'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$data = curl_exec ($ch);

curl_close ($ch);

 

Second attempt - added 999999 as device_id  and fixed syntax - added missing  }]- no change.

Third attempt - Put all the CURLOPT args into an array (like this:) and no change:

$vars = array("api_key" => "xxxxxxxxxxxxxxxxxb164",
      "events" => [{
        "device_id" => "9999999",
        "event_type" => "Sign up"}]
        );        
$headers = [
    'Content-Type: application/json',
    'Accept: */*'
];
 $ch = curl_init();
 curl_setopt_array($ch,
  array(
CURLOPT_URL => "https://api2.amplitude.com/2/httpapi",
CURLOPT_POST       => true,
CURLOPT_POSTFIELDS => $vars,
CURLOPT_RETURNTRANSFER     => true,
CURLOPT_HTTPHEADER => $headers,
)
);

$data = curl_exec ($ch);

curl_close ($ch);

 

 

Thanks in advance 
 

icon

Best answer by Robert Lefebvre 9 September 2023, 23:10

View original

1 reply

Please disregard my previous post. I found the problem and was at my end.

Reply