php - Google Analytics Measurement Protocol POST request - php

I have issue when sending POST request from my server to google analytics.
I'm trying to send test (event,order etc.), but then I do not receive anything and when I look to events tracker in browser there is absolutely nothing happening...
PHP code
$x = [
'v'=>'1',
't'=>'event',
'tid'=>'.....', // here goes my tracking ID
'cid'=>'555',
'ec'=>'video'
];
echo(google_a($x));
function google_a($x) {
$x = http_build_query($x);
$ch = curl_init();
$user_agent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL,"https://www.google-analytics.com/collect");
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$x);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
return($server_output);
}
I think that my CURL configuration isn't good. Can you help me with this?

I've solved this problem by adding this lines. They, as I understand, disable SSL connection verifying:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
So, I'm able to control google analytics from server :))

What is the response from google? $server_output
Do this:
echo "<pre>"
print_r( $server_output )
And put here que feedback

Related

PHP - Sending POST without using SOAP?

This PHP code works using SoapClient.
$client = new SoapClient("http://www.roblox.com/Marketplace/EconomyServices.asmx?WSDL");
$response = $client->GetEstimatedTradeReturnForTickets(array("ticketsToTrade" => 1000));
echo $response->GetEstimatedTradeReturnForTicketsResult;
It echoes a number.
I plan on doing this on x10hosting (or any other free web host with 10 minute cron) and x10hosting doesn't support SoapClient.
So how would this be written without using Soap?
EDIT:
So I've also tried this and it didn't work.
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.roblox.com/Marketplace/EconomyServices.asmx/GetEstimatedTradeReturnForRobux");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,array("robuxToTrade" => 1000));
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
echo $server_output
curl_close ($ch);
?>
For that specific call you can use CURL, see below. For more extensive SOAP requests you might want to look for a library to replace the missing SoapClient (see the comments under your question).
Example using CURL:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.roblox.com/Marketplace/EconomyServices.asmx/GetEstimatedTradeReturnForRobux");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "ticketsToTrade=1000");
...
Or just use other answers: PHP + curl, HTTP POST sample code?

PHP - C2DM Application Server Implementation

I'm trying to setup the application server part of C2DM push messaging using this code - https://github.com/lytsing/c2dm-php.
I have completed the app side of things and have registered an email address with Google - every time I run the code (on a server with php/cURL installed) i get the error 'get auth token error'. It driving me nuts as I've no idea where to begin to solve the problem.
The only lines I have changed in the code are - in the s2dm.php file -
'source' => 'com.phonegap.chillimusicapp',
and I added my email/password into the post.php file -
$result = $c2dm->getAuthToken("email#googlemail.com", "password");
Any advice would be great!
Cheers
Paul
Try using below sample code, It is working fine.
<?php
define("C2DM_ACCOUNT_EMAIL","[C2DM_EMAIL]");
define("C2DM_ACCOUNT_PASSWORD","[C2DM_PASSWORD]");
define("C2DM_CLIENT_LOGIN_URL","https://www.google.com/accounts/ClientLogin");
define("C2DM_MSG_SEND_URL","https://android.apis.google.com/c2dm/send");
function sendPushNotification($device_reg_id,$msg){
$auth_id=get_auth_id(); // To get Auth ID
$post_fields=array(
'collapse_key=ck_1',
'registration_id='. trim($device_reg_id),
'data.payload='. trim($msg),
);
$data_str=implode('&', $post_fields);
$headers = array(
'Authorization: GoogleLogin auth='.trim($auth_id),
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.trim(strlen($data_str)),
'Connection: close'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,C2DM_MSG_SEND_URL);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// print_r($server_output);
}
function get_auth_id(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,C2DM_CLIENT_LOGIN_URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Email=".C2DM_ACCOUNT_EMAIL."&Passwd=".C2DM_ACCOUNT_PASSWORD."&accountType=GOOGLE&source=Google-cURL-Example&service=ac2dm");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// print_r($server_output);
$parts=explode("Auth=",$server_output);
$auth_id=$parts[1];
// echo $auth_id;
return $auth_id;
}
$reg_id = "[DEVICE_REG_ID]";
sendPushNotification($reg_id,"Hello World...!! Jay is testing C2DM...");
FYI! No need to call get_auth_id() every time you send notification, You can call once and store auth_id somewhere in config file also.

See what CURL sends from a PHP script

I'm having dificulties to query a webform using CURL with a PHP script. I suspect, that I'm sending something that the webserver does not like. In order to see what CURL realy sends I'd like to see the whole message that goes to the webserver.
How can I set-up CURL to give me the full output?
I did
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
but that onyl gives me a part of the header. The message content is not shown.
Thanks for all the answers! After all, they tell that It's not possible. I went down the road and got familiar with Wireshark. Not an easy task but definitely worth the effort.
Have you tried CURLINFO_HEADER_OUT?
Quoting the PHP manual for curl_getinfo:
CURLINFO_HEADER_OUT - The request string sent. For this to work, add
the CURLINFO_HEADER_OUT option to the handle by calling curl_setopt()
If you are wanting the content can't you just log it? I am doing something similar for my API calls
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::$apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, count($dataArray));
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$logger->info("Sending " . $dataString);
self::$results = curl_exec($ch);
curl_close($ch);
$decoded = json_decode(self::$results);
$logger->debug("Received " . serialize($decoded));
Or try
curl_setopt($ch, CURLOPT_STDERR, $fp);
I would recommend using curl_getinfo.
<?php
curl_exec($ch);
$info = curl_getinfo($ch);
if ( !empty($info) && is_array($info) {
print_r( $info );
} else {
throw new Exception('Curl Info is empty or not an array');
};
?>

TTY and/or ARF Response from Experian's API

I'm trying to use the PHP Curl library to connect to Experian's API.
When I post a HTTPS request to Experian, I get an HTTP 200 OK response, but nothing more. I am expecting a TTY or ARF response. Do anyone have insight in what I'm doing wrong?
Here's a snippet of my code below for reference
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://stg1.experian.com/lookupServlet1?lookupServiceName=AccessPoint&lookupServiceVersion=1.0&serviceName=NetConnectDemo&serviceVersion=2.0&responseType=text/plain'); //not the actual site
//For Debugging
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT,60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
//#2-Net Connect client receives ECALS response. This is the Net Connect URL.
$ecals_url = curl_exec($ch);
//#3-Net Connect client validates that the URL ends with “.experian.com”. If the URL is valid, the processing continues; otherwise, processing ends.
$host_name = parse_url( $ecals_url );
$host_name = explode(".", $host_name['host'] );
$host_name = $host_name[1].'.'.$host_name[2];
if( $host_name == "experian.com" )
{
//#4-Net Connect client connects to Experian using URL returned from ECALS.
echo "step 4 - connect to secure connection<br>";
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch,CURLOPT_URL,$ecals_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
curl_setopt($ch,CURLOPT_CERTINFO,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //times out after 10s
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec ($ch);
print_r ($result);
It has been a while since I messed with our Experian Net Connect service but I believe you have to base64 encode the username:password value for their system to take it.

Using SMS Gateway API in PHP

i couldnt send sms using the following code, but i can sent sms using the same url, while i paste the url($murl) it into browser address bar
connection timed out, takes too much time to execute, but no result
what is the problem?
$amount="500";
$d="23-03-09";
$mNumber="98689988898";
$mName="TEST";
$mMessage ="\"We have debited Rs.$amount. Your account on $d. Thank you for your valuable support.";
$u1 = 'http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?';
$u2= 'username='.urlencode('some').'&password='. urlencode('some').'&sendername='.urlencode('some') .'&mobileno='
. urlencode($mNumber).'&message='.urlencode($mMessage).'&submit=Submit';
$murl=$u1.$u2;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $murl);
//curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
/*curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $u2);
*/$response = curl_exec($ch);
print "Respons : $response";
curl_close($ch);
mysmsmantra is now available as a drupal module you can use the same with triggers and actions the module can be found at http://drupal.org/project/sms_mysmsmantra
Change your code to this. Should work:
FROM
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $murl);
TO
$ch = curl_init($murl);
You can set the URL in the curl_init function as well.
You can also omit curl with $response = file_get_contents($murl) if you don't need server side header answers. Check also http_build_query().
Perhaps you need to set a user agent. The service you're using could be blocking the default user agent:
curl_setopt($ch, CURLOPT_USERAGENT, 'SMS Gateway Agent/1.0'); // Pick something creative, or use a browser UA
Hope this helps!
Looking at the symptoms, I think this is an issue with firewall/access. Have you tried a script that just gets a page from the same site just to see that there is no firewall/proxy setting blocking the access. You could just use a command line web browser such as lynx to access the site from the server you are running the script to check whether the server is allowing the request to go out.
If you are game enough, run some packet sniffers to see whether any request packets are going out from the server.
<?php
if(isset($_POST['submit'])){
$message= rawurlencode($_POST['message']);
$phone=$_POST['phone'];
$url='http://sms.yourdomain.com/httpapi/smsapi?uname=xxxx&password=******&sender=XXXXX&receiver='.$phone.'&route=TA&msgtype=1&sms='.$message;
$ch = curl_init();
$header = array("Content-Type:application/json", "Accept:application/json");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_POST, 1);
// response of the POST request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responseBody = json_decode($response);
curl_close($ch);
}
?>
<form action="sms.php" method="post">
Phone: <input type="text" name="phone"><br>
Message: <input type="text" name="message"><br>
<input type="submit" name="submit" value="sent">
</form>

Categories