Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Measurement protocol returns GIF89a�����,D;

I'm saving impressions using the Google Measurement protocol API. It works fine but although i'm not echoing anything it returns GIF89a�����,D; which is visible on the page. Any idea how I can hide this?

I'm sending the data using a HTTP post request.

$url = 'http://www.google-analytics.com/collect';
$fields_string = '';
$fields = array(
    'v' => 1,
    'tid' => 'UA-xxxxxxx-xx',
    'cid' => xxx,
    't' => 'event',
    'ec' => 'category',
    'ea' => 'impression',
    'el' => 'label'
);

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

$result = curl_exec($ch);
curl_close($ch);
like image 454
Mirko Avatar asked Feb 11 '23 11:02

Mirko


1 Answers

As it stands, when doing your cURL call the returned information will be displayed on the screen. If you want to avoid that you can use CURLOPT_RETURNTRANSFER, which will allow you to store the returned information to a variable like you did.

$result = curl_exec($ch);

Since you're not "telling" curl otherwise it will simply spill the return to the browser.

So finally, you curl part should lok like this:

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url); //curl the url
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE); //return the transfer to be later saved to a variable
$result = curl_exec($ch);
like image 99
Andrei Avatar answered Feb 13 '23 03:02

Andrei