how to find out keyword position on google with php - php

How can I find out my keyword position on Google with PHP ?
I tried this URL :
http://www.google.com/url?sa=t&source=web&ct=res&cd=7&url=http%3A%2F%2Fwww.example.com%2Fmypage.htm&ei=0SjdSa-1N5O8M_qW8dQN&rct=j&q=flowers&usg=AFQjCNHJXSUh7Vw7oubPaO3tZOzz-F-u_w&sig2=X8uCFh6IoPtnwmvGMULQfw
but I couldn't get any html source code.
How can I do this ?

I use this script I made.
As it relies on possibly changing html from google it is not reliable but does it's job for now :
<?php
// Include the phpQuery library
// Download at http://code.google.com/p/phpquery/
include("phpQuery-onefile.php");
$country = "en";
$domain = "stackoverflow.com";
$keywords = "php google keyword rank checker";
$firstnresults = 50;
$rank = 0;
$urls = Array();
$pages = ceil($firstnresults / 10);
for($p = 0; $p < $pages; $p++){
$start = $p * 10;
$baseurl = "https://www.google.com/search?hl=".$country."&output=search&start=".$start."&q=".urlencode($keywords);
$html = file_get_contents($baseurl);
$doc = phpQuery::newDocument($html);
foreach($doc['#ires cite'] as $node){
$rank++;
$url = $node->nodeValue;
$urls[] = "[".$rank."] => ".$url;
if(stripos($url, $domain) !== false){
break(2);
}
}
}
print "Country: ".$country."\n";
print "Domain: ".$domain."\n";
print "Keywords: ".$keywords."\n";
print "Rank: ".$rank."\n";
print "First urls:\n";
print implode("\n", $urls)."\n";
?>

Maybe you should just use a class written for this purpose, for example this one on github

Related

How to fix the endless work of my parser on PHP and CURL?

I need to make a parser that will read the specified number of links for example 80. I can’t figure out how to save or read all the links displayed on the screen. Now the parser is working endlessly.
<?php
$page = 1;
$var = array();
$total = fPars('https://www.dns-shop.ru/catalog/17a892f816404e77/noutbuki/?p=1&order=1&groupBy=none&stock=2', $page, $var);
function fPars($parsLink, $page, &$var){
$arr = parse_url($parsLink);
parse_str($arr['query'],$query);
$query['p'] = $page;
$query = http_build_query($query);
$url = $arr['scheme'].'://'.$arr['host'].$arr['path'].'?'.$query;
$html = curl_get($url);
$dom = str_get_html($html);
$links = $dom->find('.product-info__title-link');
$page = $page +1;
foreach ($links as $link) {
echo $link . '<br/>';
}
$var = array_merge($var,$links);
echo count($var) . '<br/>';
if (count($var) < 80) {
fPars($url, $page, $var);
}
}
?>

Read contents of directory, output results randomly with HTML tag title php (working but not title)

So far my script is working fine, basically it gets all htm files, out puts results, however im using DOM to get the HTML title tag from each file, that's where im not get to get it in the random array.. (image basenames and htm basename files are the same (firstresult.htm has picture firstresult.jpg)
I hope the code I provide and answer will be useful
<?php
// loop through the images
$count = 0;
$filenamenoext = array();
foreach (glob("/mydirectory/*.htm") as $filename) {
$filenamenoext[$count] = basename($filename, ".htm");
$count++;
}
for ($i = 0; $i < 10; $i++) {
$random = mt_rand(1, $count - 1);
$cachefile = "$filename";
$contents = file($cachefile);
$string = implode($contents);
$doc = new DOMDocument();
#$doc->loadHTML($string);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
echo '<img class="image" src="'.$filenamenoext[$random].'.jpg" " />"'.$title.'"<BR><BR>';
}
?>
It looks like the $filename variable you use on the line $cachefile = "$filename"; hasn't been set. It's only defined in the foreach loop's scope.
You should change it to
$cachefile = $filenamenoext[$random] . '.htm';
Also, it's a better practice to use array_push() and count() functions, instead of using a counter and manually filling the array. At least the code is better looking and more readable.
<?php
// loop through the images
$count = 0;
$filenamenoext = array();
foreach (glob("/mydirectory/*.htm") as $filename) {
array_push($filenamenoext, basename($filename, ".htm"));
}
for ($i = 0; $i < 10; $i++) {
$random = mt_rand(1, count($filenamenoext) - 1);
$cachefile = $filenamenoext[$random] . '.htm';
$contents = file($cachefile);
$string = implode($contents);
$doc = new DOMDocument();
#$doc->loadHTML($string);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
echo '<img class="image" src="' . $filenamenoext[$random] . '.jpg" " />"' . $title . '"<BR><BR>';
}
?>

Display most recent additions to XML file?

I'm trying to display the latest additions to this NVD XML file:
http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-recent.xml
I can get all of them to list using the following code, but I'm only interested in displaying the most recent ten (from 2013 for the time being) and the XML file lists them in chronological order (starting in 2011).
<?php
$file= 'http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-recent.xml';
$xml = file_get_contents($file);
$sxe = new SimpleXMLElement($xml);
$ns = $sxe->getNamespaces(true);
echo "<b>Latest Vulnerabilities:</b><p>";
foreach($sxe->entry as $entry)
{
$vuln = $entry->children($ns['vuln']);
$href = $vuln->references->reference->attributes()->href;
echo "" . $vuln->{'cve-id'} . "<br>";
}
?>
Since you cannot manipulate the XML arrays directly, something like this should work for your needs:
$file= 'http://static.nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-recent.xml';
$xml = file_get_contents($file);
$sxe = new SimpleXMLElement($xml);
$ns = $sxe->getNamespaces(true);
echo "<b>Latest Vulnerabilities:</b><p>";
$all = $sxe->entry;
$length = count($all);
$offset_start = $length - 10;
for($i = 0; $i < $length; $i++)
{
if($i >= $offset_start)
{
$entry = $all[$i];
$vuln = $entry->children($ns['vuln']);
$href = $vuln->references->reference->attributes()->href;
echo "" . $vuln->{'cve-id'} . "<br>";
}
}

Getting last tweet with PHP

To get my last tweet in PHP I use this code :
function getTweets($tweetsToDisplay, $user_id) {
$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?user_id=' . $user_id . '&include_rts=true&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci, CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);
curl_close($twitterci);
return ($twitterinput);
}
$user_id = '99999999';
$var = json_decode(getTweets(1, $user_id));
$txt = $var[0]->text;
$txt = preg_replace('%(https?://)[^\s]*%', '$1...', $txt);
echo $txt;
Works fine but I want to get the date as well. How to extract it ?
I hope code below help you.
function getTimeline($count, $username) {
$url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.$username.'&count=.'$count;
$tweets = json_decode(file_get_contents($url),TRUE);
return $tweets;
}
You can try this simple PHP function that I created to catch latest tweets easily (does not require API authentication). Must be optimized :)
function getTweets($user, $count) {
$datas = file_get_contents('https://twitter.com/'.$user);
preg_match_all('/<p class="js-tweet-text tweet-text">(.*?)<\/p>/', $datas, $matchetweets);
$matchetweets[1] = preg_replace('/<s>(.?)<\/s>/', '$1', $matchetweets[1]);
$matchetweets[1] = preg_replace('/(class|dir|rel|data-expanded-url|data-pre-embedded|data-query-source)="(.*?)"/', '', $matchetweets[1]);
$matchetweets[1] = preg_replace('!\s+!', ' ', $matchetweets[1]);
for ($i = 1; $i <= $count; $i++) {
echo '<li>'.$matchetweets[1][$i].'</li>'."\n";
}
};
Usage
echo getTweets('nasa', 3);
UPDATE (10/15/2014) :
This version is out of date and not working anymore. Here is an updated PHP code to parse tweets easily.
function getTweets($user, $count) {
$datas = file_get_contents('https://mobile.twitter.com/'.$user);
preg_match_all('/<div class="tweet-text" data-id="\d*">(.*?)<\/div>/', $datas, $matchetweets);
$matchetweets[1] = preg_replace('/<div class="dir-ltr" dir="ltr">/', '', $matchetweets[1]);
for ($i = 1; $i <= $count; $i++) {
echo '<li>'.$matchetweets[1][$i].'</li>'."\n";
}
};
UPDATE (5/30/2015) :
function getTweets($user, $count) {
$datas = file_get_contents('https://mobile.twitter.com/'.$user);
preg_match_all('/<div class="tweet-text" data-id="\d*">(.*?)<\/div>/s', $datas, $matchetweets, PREG_SET_ORDER);
for ($i = 1; $i <= $count; $i++) {
$matchetweets[$i][0] = preg_replace('/<div class="dir-ltr" dir="ltr">/', '', $matchetweets[$i][0]);
$matchetweets[$i][0] = preg_replace('/\s+/', ' ', $matchetweets[$i][0]);
$matchetweets[$i][0] = str_replace('"> ', '">', $matchetweets[$i][0]);
echo '<li>'.$matchetweets[$i][0].'</li>'."\n";
}
};
Usage doesn't change. Minimum 1 tweet, maximum 20 tweets.
<?php
$date = $var[0]->created_at;
That should work!
Add this to bottom of your code.
$time = $var[0]->created_at;
echo $time;
Based on the Twitter API results from http://dev.twitter.com/doc/get/statuses/user_timeline, it appears that you could use the created_at param.
You have:
$txt = $var[0]->text;
If that works then add
$created = $vars[0]->created_at;
echo $txt;
echo "<span>".$created."</span>" ;

PHP pick tweets code

I have a php code that fetches my latest tweet and displays it on my website. It works great but I would like to know if I could do this but maybe fetching my 5 last tweets instead of only the latest. This is my code for twitter.php:
function returnTweet()
{
$username = "username";
$prefix = "<div><big><i>#$username ";
$suffix = "</i></big></div>";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=5";
$twitterFeed = file_get_contents($feed);
$tweet = parse_feed($twitterFeed);
return $prefix.$tweet.$suffix;
}
function parse_feed($feed)
{
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
And i display it in my php page like this:
include "twitter.php";
echo "".returnTweet();
Would appreciate any help in this!
Regards from Paparappa
You're much better off using an XML-parser for this (for example: http://www.php.net/manual/en/simplexml.examples-basic.php).
(not sure about the structure of twitter-atoms, so $xml->tweet[$i] is probably something else..)
$xml = new SimpleXMLElement($twitterFeed);
$tweets = array();
for ($i = 0; $i < 5; $i++) {
$tweets[] = $xml->tweet[$i];
}

Categories