我的API目前通过cURL请求发送分析跟踪,但是,这确实减慢了API可以处理的请求数量,因此我尝试通过套接字发送它而忽略输出,但分析并不是似乎正在跟踪它。
我已经在下面添加了两组代码,当使用curl时,我可以看到它立即显示在分析中实时,当使用套接字时,实时分析并不是真正的分析变化
我不确定套接字代码是否存在错误,或者谷歌分析是否因为某种原因而不喜欢它,有什么想法?
套接字代码:
private function track() {
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
$url = 'www.google-analytics.com';
$page = '/collect';
$fields = array(
'v' => '1',
'tid' => $this->GA_ID,
'cid' => $this->gaParseCookie(),
't' => 'pageview',
'dh' => 'webservice.fanart.tv',
'dp' => $this->ttype.' - '.$_GET["api_key"].' - '.$this->project,
'dt' => $this->tid,
'uip' => $_SERVER['REMOTE_ADDR']
);
$fields_string = http_build_query($fields);
$fp = fsockopen($url, 80, $errno, $errstr, 5);
$output = "POST $page HTTP/1.1\r\n";
$output .= "Host: $url\r\n";
$output .= "Content-Type: application/x-www-form-urlencoded\r\n";
$output .= "Content-Length: ".strlen($fields_string)."\r\n";
$output .= "Connection: close\r\n";
$output .= $fields_string;
fwrite($fp, $output);
fclose($fp);
}
卷曲代码:
private function track() {
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
$url = 'http://www.google-analytics.com/collect';
$fields = array(
'v' => '1',
'tid' => $this->GA_ID,
'cid' => $this->gaParseCookie(),
't' => 'pageview',
'dh' => 'webservice.fanart.tv',
'dp' => $this->ttype.' - '.$_GET["api_key"].' - '.$this->project,
'dt' => $this->tid,
'uip' => $_SERVER['REMOTE_ADDR']
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
}
答案 0 :(得分:1)
不出所料,这最终只是一个简单的错误。
$output .= "Connection: close\r\n";
$output .= $fields_string;
需要
$output .= "Connection: close\r\n\r\n";
$output .= $fields_string;
跟踪开始工作后
此外,我发现在某些主机上查找主机名导致它速度变慢,在保存内存中的IP后,便宜的VPS我进行了单元测试,从平均每秒15次请求到每秒约1,000次请求。 / p>
更新的工作代码如下:
public function track() {
$url = 'www.google-analytics.com';
$page = '/collect';
$googleip = $this->memcacheget('googleip');
if(empty($googleip)) {
$googleip = gethostbyname($url);
$this->memcacheset('googleip', $googleip, 3600);
}
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
$fields = array(
'v' => '1',
'tid' => $this->GA_ID,
'cid' => $this->gaParseCookie(),
't' => 'pageview',
'dh' => 'webservice.fanart.tv',
'dp' => $this->ttype.' - '.$_GET["api_key"].' - '.$this->project,
'dt' => $this->tid,
'uip' => $_SERVER['REMOTE_ADDR'],
'ua' => $_SERVER['HTTP_USER_AGENT']
);
$fields_string = http_build_query($fields);
$fp=fsockopen($googleip, 80, $errno, $errstr, 5);
stream_set_blocking($fp, 0);
stream_set_timeout($fp, 5);
$output = "POST http://".$url.$page." HTTP/1.1\r\n";
$output .= "Host: $url\r\n";
$output .= "Content-Length: ".strlen($fields_string)."\r\n";
$output .= "Connection: close\r\n\r\n";
$output .= $fields_string;
$sentData = 0;
$toBeSentData = strlen($output);
while($sentData < $toBeSentData) {
$sentData += fwrite($fp, $output);
}
fclose($fp);
}
public function memcacheget($key){
$memcache = new Memcache;
$memcache->connect('localhost', 11211);
$result = $memcache->get($key);
return $result;
}
public function memcacheset($key,$value,$timeout=86400){
$memcache = new Memcache;
$memcache->connect('localhost', 11211);
//$memcache->flush();
$result = $memcache->get($key);
if(empty($result)){ //store in memcache
$memcache->set($key,$value,MEMCACHE_COMPRESSED,$timeout);
} else {
$memcache->replace($key,$value,MEMCACHE_COMPRESSED,$timeout);
}
return $result;
}