从 PHP 发送 POST 批量请求
Send POST Batch Request From PHP
Measurement protocol docs 给我这些指示以发送批量请求:
POST /batch HTTP/1.1
Host: www.google-analytics.com
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact
我对如何为此构建 url 有点困惑?
我尝试过的:
$guzzle = new \GuzzleHttp\Client();
$guzzle->request('POST','www.google-analytics.com/batch',[
'query' => "v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event"
]);
这不起作用,ga 中应该有 5 个新事件,但我只收到 1 个(第一个)。
如何向测量协议发送批量请求?
也许 url 需要在数据部分,而不是在请求的 header 部分?
所以使用laracurl的第二个post参数(来自Laracurl README):
$response = Laracurl::post($url, ['post' => $data]);
与 :
$data = join("\n",array(
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome",
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout",
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact"));
如果你使用GuzzleHttp
,你应该使用body
而不是query
:
$guzzle = new \GuzzleHttp\Client();
$guzzle->request('POST','www.google-analytics.com/batch', [
'body' => implode("\n", array(
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
)]);
希望对某人有所帮助:Analytics Management API v3
创建新目标
(PHP Google_Client, Google_Service_Analytics)
/*
Assuming there is a $goals array
*/
$client = new Google_Client();
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->addScope([Google_Service_Analytics::ANALYTICS_READONLY,
Google_Service_Analytics::ANALYTICS_EDIT]);
$client->setDeveloperKey($API_KEY);
$client->setSubject($EMAIL);
$client->refreshToken($REFRESH_TOKEN);
$client->setUseBatch(true);
$analytics = new Google_Service_Analytics($client);
$batch = $analytics->createBatch();
foreach($goals as $goal){
$req1 = $analytics->management_goals->insert(XXXXXX, UA-XXXXXX-1, XXXXXX, $goal);
$batch->add($req1);
}
try {
$batchResponse = $batch->execute();
/* Handling Response */
foreach ($batchResponse as $key => $value) {
if(!($value instanceof Google_Service_Exception)){
echo $value->getId()."\n";
continue;
}
print_r($value->getErrors());
}
} catch (Google_Service_Exception $e) {
/*
handling exception
*/
}
Google 阻止带有 guzzle header 的请求。
要成功发送请求,您需要替换 header
'headers' => [
'User-Agent' => 'Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101',
],
Measurement protocol docs 给我这些指示以发送批量请求:
POST /batch HTTP/1.1
Host: www.google-analytics.com
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact
我对如何为此构建 url 有点困惑?
我尝试过的:
$guzzle = new \GuzzleHttp\Client();
$guzzle->request('POST','www.google-analytics.com/batch',[
'query' => "v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event
v=1&tid=UA-XXXXX-Y&cid=555&t=event"
]);
这不起作用,ga 中应该有 5 个新事件,但我只收到 1 个(第一个)。
如何向测量协议发送批量请求?
也许 url 需要在数据部分,而不是在请求的 header 部分?
所以使用laracurl的第二个post参数(来自Laracurl README):
$response = Laracurl::post($url, ['post' => $data]);
与 :
$data = join("\n",array(
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome",
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout",
"v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact"));
如果你使用GuzzleHttp
,你应该使用body
而不是query
:
$guzzle = new \GuzzleHttp\Client();
$guzzle->request('POST','www.google-analytics.com/batch', [
'body' => implode("\n", array(
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
'v=1&tid=UA-XXXXX-Y&cid=555&t=event',
)]);
希望对某人有所帮助:Analytics Management API v3
创建新目标
(PHP Google_Client, Google_Service_Analytics)
/*
Assuming there is a $goals array
*/
$client = new Google_Client();
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->addScope([Google_Service_Analytics::ANALYTICS_READONLY,
Google_Service_Analytics::ANALYTICS_EDIT]);
$client->setDeveloperKey($API_KEY);
$client->setSubject($EMAIL);
$client->refreshToken($REFRESH_TOKEN);
$client->setUseBatch(true);
$analytics = new Google_Service_Analytics($client);
$batch = $analytics->createBatch();
foreach($goals as $goal){
$req1 = $analytics->management_goals->insert(XXXXXX, UA-XXXXXX-1, XXXXXX, $goal);
$batch->add($req1);
}
try {
$batchResponse = $batch->execute();
/* Handling Response */
foreach ($batchResponse as $key => $value) {
if(!($value instanceof Google_Service_Exception)){
echo $value->getId()."\n";
continue;
}
print_r($value->getErrors());
}
} catch (Google_Service_Exception $e) {
/*
handling exception
*/
}
Google 阻止带有 guzzle header 的请求。 要成功发送请求,您需要替换 header
'headers' => [
'User-Agent' => 'Mozilla: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101',
],