Мне нужно использовать этот URL (ниже) в PHP, чтобы получить ответ API. Каждый раз, когда я пытался его использовать, я получаю сообщение об ошибке «HTTP 400: Bad Request (невозможно создать определение из аргументов)», однако он работает с использованием CURL из командной строки.
$ch = curl_init();
$location = "'".'\{"event_type":"api_SendStore","filters":\[\{"subprop_type":"event","subprop_key":"api_response","subprop_op":"is","subprop_value":\["(none)","0"\]\},\{"subprop_type":"event","subprop_key":"country","subprop_op":"is","subprop_value":\["(none)","0"\]\}\],"group_by":\[\{"type":"event","value":"platform"\}\]\}&m=uniques&start=20190401&end=20190408&i=1'."'";
//Api connection
$url = "https://amplitude.com/api/2/events/segmentation?e={$location}";
print_r($url);
//Your username.
$username = '123456';
//Your password.
$password = '889788';
//Initiate cURL.
$ch = curl_init($url);
//Specify the username and password using the CURLOPT_USERPWD option.
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
//Tell cURL to return the output as a string instead
//of dumping it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Execute the cURL request.
$response = curl_exec($ch);
print_r($response);
//Check for errors.
if(curl_errno($ch)){
//If an error occured, throw an Exception.
throw new Exception(curl_error($ch));
}
//Print out the response./echo $response;
$array = json_decode($response, true);
print_r($array);
Это команда Curl, которая работает:
curl --insecure -u API KEY:SECRET KEY 'https://amplitude.com/api/2/events/segmentation?e=\{"event_type":"api_SendStore","filters":\[\{"subprop_type":"event","subprop_key":"api_response","subprop_op":"is","subprop_value":\["(none)","0"\]\},\{"subprop_type":"event","subprop_key":"country","subprop_op":"is","subprop_value":\["(none)","0"\]\}\],"group_by":\[\{"type":"event","value":"wu_platform"\},\{"type":"event","value":"country"\}\]\}&m=uniques&start=20190401&end=20190408&i=1'