Posting metric to google analytics from perl script - perl

I need to generate a google analytic event from my perl cron job.
Based on https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
POST to www.google-analytics.com/collect will accept the metrics.
I tried posting metric using following code, but I am not seeing them in google analytics realtime page.
use LWP::UserAgent;
$ua=LWP::UserAgent->new;
$ua->default_header("Content-type" => "application/x-rm-urlencoded");
my $response = $ua->post("http://www.google-analytics.com/collect",
"v" => 1,
"tid" => "UA-XXXXXXXX-Y",
"cid" => 125,
"t" => "pageview",
"dh" => "Cron",
"dp" => "Cron.php",
"dt" => "CronTitle");
print $response->status_line;'
Response is "200 OK", but I am not seeing the data in google analytics webpage.

I am able to post analytics from my php scripts, but could not post analytics from perl script.
I solved my problem as follows.
Created an utility php script and called that php script from my perl script. Then it worked.
<?php
$ec = $_GET['ec'];
$ea = $_GET['ea'];
$el = $_GET['el'];
$ev = $_GET['ev'];
// Send event tracking as well.
$url = 'http://www.google-analytics.com/collect';
$event_data = array(
'v' => '1',
'tid' => 'UA-XXXXXX-Y',
'cid' => $_SERVER["REMOTE_ADDR"],
't' => 'event',
'ec' => $ec,
'ea' => $ea,
'el' => $el,
'ev' => $ev);
$event_options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($event_data),
),
);
$event_context = stream_context_create($event_options);
$result = file_get_contents($url, false, $event_context);
echo "XXX";
?>

Related

Turn off google assistant mic

I'm implementing an app in "Dialog Flow"
I'm sending request to app like this
$text = "Something";
$data = array(
"source" => $text,
"speech" => $text,
"displayText" =>$text,
"contextOut" => array()
);
header('Content-Type: application/json');
echo json_encode($data);
The text displayed in app. but mic is open I want turn off the mic .
I tried expectUserResponse but not working
array(
"expectUserResponse" => false,
"source" => $text,
"speech" => $text,
"displayText" =>$text,
"contextOut" => array()
)
Please help.
The expectUserResponse parameter is not part of the Dialogflow response JSON. Instead, it is part of the Actions on Google specific part of the response. If you're using Dialogflow v1, this will be in the data.google object. If you're using Dialogflow v2, this will be in the payload.google object.
So if you're using Dialogflow v1, your code might look something like this:
array(
"speech" => $text,
"displayText" =>$text,
"contextOut" => array(),
"data" => array(
"google" => array(
"expectUserResponse": false
)
)
)
while v2 might look like
array(
"speech" => $text,
"displayText" =>$text,
"contextOut" => array(),
"payload" => array(
"google" => array(
"expectUserResponse": false
)
)
)

Error test upload php unit symfony2

Im following the tutorial from symfony's website to make my phpunit's test for my controller.
Im trying to test an upload on a form and i have 3 fields on this form : title,description and file.
I use :
public function testScenarioAdd() {
$client = static::createClient();
$x_wsse = 'UsernameToken Username="username#fai.fr", PasswordDigest="aeirugbjcUbfmùJK", Nonce="OTMzOGMwYzFkYTk2MzJmYzBh", Created="2013-11-12T10:22:15+01:00"';
//X_Wsse is for the connection systeme on my application.
$image = new UploadedFile(
'/past/to/your/images.jpg',
'images.jpg',
'image/jpeg',
100000
);
$crawler = $client->request('POST', '/ad/create', array('ad_form' => array('title' => 'test', 'description' => 'Test description')),array(), array('CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'HTTP_X-WSSE' => $x_wsse));
$response = $client->getResponse()->getContent();
$json_response = json_decode($response);
print_r($response);
}
When i launch it I have an error saying :
{"app_error":{"success":500,"result":[{"message":"Array to string conversion",...
Am i doing something wrong ? or missing something in my code ?
Thanks for your help :)
Edit :
Thanks but, what ive done there is working on another test without upload, i still tried it and its not working. I still have the error array to string... I think it come from the fonction UplaodedFile or maybe from my controller itself because when i try the other solution from symfony's web site with
$photo = array(
'tmp_name' => '/path/to/photo.jpg',
'name' => 'photo.jpg',
'type' => 'image/jpeg',
'size' => 123,
'error' => UPLOAD_ERR_OK
);
With putting the
$crawler = $client->request('POST', '/ad/create', array('ad_form[title]' => 'test', ad_form[description] => 'test description'),array('file' => $image),array('CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'HTTP_X-WSSE' => $x_wsse));
The important part of my controller is :
if ('POST' === $request->getMethod()) {
$form->bind($request);
if ($form->isValid())
{
$Ad->preCreate();
$odm->persist($Ad);
$odm->flush();
$odm->clear();
$data = $this->listAction($request, 1, 5);
Tags::setEvent($request, $this->container, 'member.create.ad', $user->getId(), $Ad->getId());
return $data;
} else {
$data = Errors::formatJson($form, $this->container);
}
}
}
return new JsonResponse(array('ad_create' => $data));
Im kinda new in symfony, i really dont know where this error can come from...
Client::Request method expects you to provide it with one-dimensional array of data. So try this code:
<?php
//...
$crawler = $client->request('POST', '/ad/create',
array('ad_form[title]' => 'test',
'ad_form[description]' => 'Test description'),
array(),
array('CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'HTTP_X-WSSE' => $x_wsse));

Is Facebook batch request photo upload with no_story possible?

Is it?
Uploading works like a charm, but I don't want it to show up on timeline or feed.
I've tried adding no_story to each request, and to the $params which is commented out in following code:
foreach ($data['Ad']['Media'] as $medium) {
$file = $medium['tmp_name'];
$req = array(
'method' => 'POST',
'relative_url' => '/me/photos',
'message' => $data['Ad']['title'],
'attached_files' => 'file' . $count,
'no_story' => 1 //Also tried 'true'
);
//add this request to batch
$batch[] = json_encode($req);
$params['file' . $count] = '#' . realpath($file);
$count++;
}//foreach
$params['batch'] = '[' . implode(',', $batch) . ']';
//$params['no_story'] = true;
Edit ##
See comment below.
i know you may not need the answer any more, but i was facing same issue , and after a lot of try and error i figured it out so i will post the answer may be some one face this issue again
$req = array(
'method' => 'POST',
'relative_url' => '/me/photos',
'attached_files' => 'file' . $count,
'body' => 'no_story=1'
);

SugarCRM soap call not linking contact to a target list

I'm using SugarCRM 6.4.4 and php 5.3.10 and trying to add a contact to a target list using a soap call...I've also tried using the ProspectList class directly but I can't get it to work. I know the SOAP calls are getting through because I can create the Contact just fine, but the problem is coming when I try to create the relationship between the Contact and the Target List (PropsectList). What am I doing wrong?
Here's the SOAP call I tried. I'm getting an error that says: "Call to a member function add() on a non-object in /data/servers/thesite.com/web/administrator/sales/soap/SoapSugarUsers.php on line 1350"...Here's the code around line 1350
if (!empty($key)) {
$mod->load_relationship($key);
$mod->$key->add($module2_id);
return $error->get_soap_array();
}
Here's the full code for the SOAP:
// set up options array
$options = array(
"location" => 'http://www.thesite.com/sales/soap.php',
"uri" => 'http://www.sugarcrm.com/sugarcrm',
"trace" => 1
);
//user authentication array
$user_auth = array(
"user_name" => 'theuser',
"password" => MD5('thepass'),
"version" => '.01'
);
// connect to soap server
$client = new SoapClient(NULL, $options);
// Login to SugarCRM
$response = $client->login($user_auth,'test');
$session_id = $response->id;
$user_id = $client->get_user_id($session_id);
//Add Contact to SugarCRM
$response = $client->set_entry($session_id, 'Contacts', array(
array('name' => 'first_name','value' => 'roneFirst2'),
array('name' => 'last_name','value' => 'roneLast2'),
array('name' => 'account_name','value' => 'jdam Props'),
array('name' => 'email1','value' => 'roneEmail2#gmail.com'),
array('name' => 'work_phone','value' => '1.888.888.8888'),
array('name' => 'description','value' => 'This is the message'),
array('name' => 'assigned_user_id','value' => $user_id)
));
$contact_id = $response->id;
//Add Contact to TARGET LIST
$relationship = array(
'module1' => 'Contacts',
'module1_id' => "$contact_id",
'module2' => 'ProspectLists',
'module2_id' => "24053784-f8d3-22a4-5e99-4fc6a7d5159f" //Note: this id was pulled from the table in the database directly for the target list we want to link the contact with.
);
//call set_relationship()
$response = $client->set_relationship($session_id, $relationship);
been googleing and trying to figure this out for 3 days now...Thanks for any help you are willing to give.

POST web authentication of Nest Thermostat site in Perl (attempting to convert from Ruby)

there is a bit of code I'm trying to replicate in Perl using either LWP::UserAgent or WWW::Mechanize from an existing script.
The original script actually does more than I'm looking to do. I'd just like to log into the Nest website (the part I need help with) and then parse out some data for historical logging (I'm good there).
My current script I would expect to work, but I'm not sure if the authResult/access_token from the Ruby example us actually understood/used by either Perl module.
My code in Perl:
#!/usr/bin/perl
use WWW::Mechanize;
#use HTTP::Request::Common qw(POST);
use HTTP::Cookies;
use LWP::UserAgent;
use Data::Dumper;
use CGI;
my $email; #stores our mail
my $password; #stores our password
my $user_agent = 'Nest/1.1.0.10 CFNetwork/548.0.4';
$email = "email#email";
$password = "mypassword";
my #headers = (
'User-Agent' => 'Nest/1.1.0.10 CFNetwork/548.0.4',
'X-nl-user-id' => $email,
'X-nl-protocol-version' => '1',
'Accept-Language' => 'en-us',
'Connection' => 'keep-alive',
'Accept' => '*/*'
);
# print "Content-type: text/html\n\n";
my $cookie = HTTP::Cookies->new(file => 'cookie',autosave => 1,);
my $browser = WWW::Mechanize->new(cookie_jar => $cookie, autocheck => 1,);
# tell it to get the main page
$browser->get("https://home.nest.com/user/login");
print Dumper($browser->forms);
# okay, fill in the box with the name of the
# module we want to look up
$browser->form_number(1);
$browser->field("username", $email);
$browser->field("password", $password);
$browser->submit();
print $browser->content();
When I submit the form, I just get the same page returned back to me, and I don't know what exactly is causing Nest to not like what I'm submitting. There are two additional fields in the form on their log-in page:
'inputs' => [
bless( {
'maxlength' => '75',
'/' => '/',
'value_name' => 'E-mail address',
'name' => 'username',
'id' => 'id_username',
'type' => 'text'
}, 'HTML::Form::TextInput' ),
bless( {
'/' => '/',
'value_name' => 'Password',
'name' => 'password',
'id' => 'id_password',
'type' => 'password',
'minlength' => '6'
}, 'HTML::Form::TextInput' ),
bless( {
'readonly' => 1,
'/' => '/',
'value_name' => '',
'value' => '',
'name' => 'next',
'type' => 'hidden'
}, 'HTML::Form::TextInput' ),
bless( {
'readonly' => 1,
'/' => '/',
'value_name' => '',
'value' => 'dbbadca7910c5290a13d30785ac7fb79',
'name' => 'csrfmiddlewaretoken',
'type' => 'hidden'
}, 'HTML::Form::TextInput' )
Do I need to use the csrfmiddlewaretoken value in each submission? It appears to change. I thought getting a cookie upon a successful login would be enough.
Any suggestions on what I'm doing wrong?
Shot in the blue:
perl -E'use warnings; $email = "email#email"; say "<$email>"'
Possible unintended interpolation of #email in string at -e line 1.
<email>
I suspect it fails because the form gets the wrong user name, print it out to confirm. Always enable the pragmas strict and warnings to make many common mistakes visible.