我需要在PHP中使用此URL(如下)以获取API响应。每次尝试使用它时,都会收到错误消息“ HTTP 400:错误的请求(无法从参数创建定义)”,但是它可以在命令行中使用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'
答案 0 :(得分:0)
这不是创建URL或JSON的方法。两种都是特定的数据格式,并带有创建它们的适当工具。 http_build_query
构建URL查询字符串,json_encode
构建JSON。从URL中的反斜杠数量来看,您似乎正在尝试在PHP中重新创建外壳转义符。这不是必需的,当然会导致无效的URL。
<?php
$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" => "platform",
]
]
];
$e = json_encode($e);
$query = http_build_query([
"e" => $e,
"m" => "uniques",
"start" => "20190401",
"end" => "20190408",
"i" => "1"
]);
$url = "https://happyhappy.com/api/2/events/segmentation?$query";
echo $url;
输出:
https://happyhappy.com/api/2/events/segmentation?e=%7B%22event_type%22%3A%22api_SendStore%22%2C%22filters%22%3A%5B%7B%22subprop_type%22%3A%22event%22%2C%22subprop_key%22%3A%22api_response%22%2C%22subprop_op%22%3A%22is%22%2C%22subprop_value%22%3A%5B%22%28none%29%22%2C%220%22%5D%7D%2C%7B%22subprop_type%22%3A%22event%22%2C%22subprop_key%22%3A%22country%22%2C%22subprop_op%22%3A%22is%22%2C%22subprop_value%22%3A%5B%22%28none%29%22%2C%220%22%5D%7D%5D%2C%22group_by%22%3A%5B%7B%22type%22%3A%22event%22%2C%22value%22%3A%22platform%22%7D%5D%7D&m=uniques&start=20190401&end=20190408&i=1
此外,curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
真是愚蠢。如果您完全重视安全性,那就不要这样做。