I'm using a basic URL shortening script for one of my sites so that I can manage the URLs internally (and have less trailing characters on the urls). It's working well, but I'd like to be able to use Google Analytics to track the clicks.
I've found this: http://code.google.com/p/serversidegoogleanalytics/ which seems to achieve this using events, but I'm not using Zend. It mentions using curl to customise it, but without any knowledge of curl, would it simply be a case of amending this function from within the class:
public function getHttpClient () {
if(!$this->httpClient instanceof Zend_Http_Client) {
include_once("Zend/Http/Client.php");
$this->httpClient = new Zend_Http_Client();
$this->httpClient->setConfig(array(
'maxredirects' => 1,
'timeout' => 4
));
$this->httpClient->setHeaders('Referer', "http://" . self::$trackingDomain . "/");
$this->httpClient->setHeaders("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729)");
$this->httpClient->setCookieJar();
}
return $this->httpClient;
}
You should be able to convert that to curl quite easily.
Curl is basically.
get a curl handler
configure the request through the handler
do the request and grab the result if you need
Check out the examples page, it's pretty simple
http://au.php.net/manual/en/curl.examples-basic.php
EDIT: there's even examples of Analytics with curl on google..
http://www.electrictoolbox.com/google-analytics-login-php-curl-username-password/
Related
I am trying to figure out how can I call php functions running on remote of shoretel IP phone .
I can access it through browser by simple IP and it is flash player script but I want to do few things programmatically and I don't find any help to do the same.
The flash player app is calling following api
https:///gateway.php?PHPSESSID=6432fc5f0999d0f4536fced79b95
and in payload of this it is calling php function like LSRoom_Remoting.getHeaderInfo
I am trying but getting error like "Malformed AMF message"
Here is the code
static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
var content = new StringContent("LSRoom_Remoting.getBoardID", Encoding.UTF8, "application/x-amf");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Accept","*/*");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.9");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
//client.DefaultRequestHeaders.Add("Content-Length", "47");
//client.DefaultRequestHeaders.Add("Content-Type", "application/x-amf");
client.DefaultRequestHeaders.Add("Cookie", "PHPSESSID=3cbc7ff268e80e78e1458a4adfdd1f56");
client.DefaultRequestHeaders.Add("DNT", "1");
client.DefaultRequestHeaders.Add("Host", "10.6.32.21");
client.DefaultRequestHeaders.Add("Referer", "https://10.6.32.21/interface/ls_interface.swf");
client.DefaultRequestHeaders.Add("Origin", "https://10.6.32.21");
client.DefaultRequestHeaders.Add("X-Requested-With", "ShockwaveFlash/32.0.0.114");
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
var result = client.PostAsync(string.Format("https://10.6.32.21/gateway.php?PHPSESSID={0}", "3cbc7ff268e80e78e1458a4adfdd1f56"), content).Result;
//var result = client.DownloadString("https://10.6.32.21/interface/interface.php?uniqueKey=152322314997139&brand=shoretel&key=");
var obj = result.Content.ReadAsStringAsync();
Console.WriteLine(result.Content.ToString());
Console.ReadKey();
}
}
What i am doing wrong?
it seems likely, that the PHPSESSID is long expired, while not using one from a recent request; because the default timeout is 15 minutes. in case it had not yet been expired, you still have to build a proper AMF message, which consist of a header and a body. there is a library for that: amfphp, which at least should provide some clue how it should look alike, despite it is written in PHP.
simplexml_load_file() does not load XML file when the URL includes an ampersand symbol. I have tried two examples with and without ampersand:
$source1 = simplexml_load_file("http://www.isws.illinois.edu/warm/data/outgoing/nbska/datastream.aspx?id=ncu");
print_r($source1); //works
$source2 = simplexml_load_file("http://forecast.weather.gov/MapClick.php?lat=38.8893&lon=-77.0494&unit=0&lg=english&FcstType=dwml");
print_r($source2); //no output
First example works well as it does not includes ampersand, but the second example does not work as it include ampersand.
I have referenced
simplexml_load_file with & (ampersand) in url with Solr and simplexml_load_file ampersand in url but it did not work.
The issue is not the ampersand in the URL. The issue, instead, is that weather.gov appears to be blocking these types of requests. They will not allow users that do not have a useragent set.
The fastest way to get around this is to set a UserAgent within PHP, which you can do by putting this code above your xml call:
ini_set('user_agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20100101 Firefox/9.0');
However, I would recommend using CURL instead of simplexml_load_file, as simplexml_load_file is often restricted by server configuration. If you were to do this with curl, you'd want to do something like the first answer here:
SimpleXML user agent
I have tested this locally and got it working just by specifying a user agent.
EDIT: Also, welcome to SO! Be sure to vote often ;D
I wanted to know in PHP, how to detect on which browser my web application is running.
e.g.
If current browser is chrome then alert("Chrome browser processing") otherwise alert("rest browser processing");
echo $_SERVER['HTTP_USER_AGENT'];
Below output I am getting If i execute above code :
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.21 Safari/532.0
Please suggest your pointers to detect exact browser name.
Thanks,
-Pravin
This has some useful code.
Basically, in your case, you can just look for the string "Chrome". In general, it might take a bit more logic, since, for example, the string "Safari" is found in the user-agents provided by other browsers than Safari (including Chrome). PHP provides the 'browser' element for this.
Personally, I'd use Javascript for this one.
if(navigator.userAgent.match(/Chrome/i)) {
alert("You're using Chrome!");
}
else {
alert("You're using something other than Chrome!");
}
... but if you really wanted to, you could accomplish the same thing in PHP:
if (preg_match("/Chrome/i", $_SERVER['HTTP_USER_AGENT']) == 0) {
// zero matches
echo "<script>alert('You're not using Chrome!')</script>";
} else {
echo "<script>alert('You're using Chrome!')</script>";
}
How can we get Browser Name and Version information using php script?
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>
As Palantir says, additionally have a look at the get_browser function, where you can check also capabilities enabled in the browser.
http://php.net/manual/en/function.get-browser.php
You will need to create function to translate user agent data into common names of browsers
For example, $_SERVER['HTTP_USER_AGENT'] could return
Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9* is firefox
or
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.55 Safari/533.4 is Chrome
The details provide you with the rendering engine, code base, version, os, etc...
I'd suggest using preg_match and/or a list of known browsers is you want to do something like
echo browserCommonName($_SERVER['HTTP_USER_AGENT']);
to output "Google Chrome".
browserCommonName($userAgent) would need a list of known browsers.
edit: just noticed get_browser buit into php does this, my bad for not reading the thread.
See get_browser().
<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser(null, true);
print_r($browser);
?>
All in all, you can't. You can certainly try to get it, and you're almost certainly guaranteed to get something that looks like what you want; but there is absolutely no way of checking wether or not the information is correct. When you receive a user agent string, the browser on the other end could be truthful, or it could be lying. When dealing with users, always assume that it is, in fact, lying.
There is no "best way" to deal with this, but what you most likely want to do is test your site with a wide variety of browsers, use portable HTML and CSS techniques, and if you absolutely must, fill the holes with JavaScript.
Choosing what data to send to a browser based on what browser you think it is, is a Bad Idea™.
You could always take a look at the php function get_browser http://php.net/manual/en/function.get-browser.php. You will need $_SERVER['HTTP_USER_AGENT'] for this.
You may also want to take a look at Chris Schuld Browser Detection Class. http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php.html
Hello I developing a website in Joomla the website is working fine in Mozilla, Safari and chrome but when I render it in Internet explorer it gives following error.
Please enlighten me on this.....
User Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)
Timestamp: Fri, 23 Oct 2009 05:15:31 UTC
Message: HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)
Line: 0
Char: 0
Code: 0
URI: http://localhost/flagdown/index.php?option=com_companies&view=profile&TAXI_ID=a936db2fe93260547e75782e250d672c&COMPANY_NAME=asd1234&CONTACT_NAME=aa&ADDRESS=aaa&SERVICE_EMAIL=ankit#aa.com&ADMIN_EMAIL=aahorror#aa.com&DISPATCH_PHNO=5555555555&ADMIN_PHNO=5555555555&NO_OF_TAXIS=57&ALLOW_AD=&TOTAL_REBATE=0.000000&USERNAME=ankit
Sounds like you've got some Javascript that's attempting to modify the DOM before it's ready. Are you using a JS library, by any chance? They really help with this; for example, the jQuery solution would be...
$(document).ready(function() {
// Any code you put in here won't run until everything is ready.
});
Try validating your HTML using the W3C validator, to make sure you've properly closed all your elements (and also that your HTML is valid).
Then, make sure you're not interfering with the DOM before the page is fully loaded.
To know more we'd have to see your HTML source code, and any JavaScript (particularly any JS that manipulates the DOM).
The problem may be related to malformed XHTML check out this blog post for more info.
If you haven't solved this with validating your HTML and using:
window.onload = function() { /* code here instead so you are sure dom is complete */ }
or
$(document).ready(function() {
// Any code you put in here won't run until everything is ready.
});
Then you may want to set a Timeout.
eg:
$(document).ready(function() {
setTimeout(function() {
// Any code you put in here won't run until everything is ready.
}, 500);
});
Sometimes IE needs just a lil' more time.