¤ is evil !

I ran into a funny problem last week with a url from the api of a payment gateway that contained …&currencyCode=…..

I adopted an orphaned codebase and am reprogramming it as WordPress plugin and it contains some really weird bugs. The two previous coders that worked on the codebase opted for an iframe solution through an intermediate page.

Problem is SimpleXml recognizing &curren and converting to ¤ (unicode U+000A4) with ¤cycode= as result, if you tried to relocate the page to that url the gateway rejects the url.

str_replace() does not recognize ¤, the solution I found is first running htmlentities() that replaces ¤ with &curren but also converts & to & which the gateway also rejects, so after using htmlentities() you use str_replace() to replace & with &, and then it works fine.

I am open to suggestions on that one, for now, conclusion is that SimpleXML does not handle &curren in a url correctly, you might run into the same problem with &current_user,  &current_time, et cetera.

google seo tips and tricks

google suggest scraper (php & simplexml)

Today’s goal is a basic php Google Suggest scraper because I wanted traffic data and keywords for free.

Before we start :

google scraping is bad !

Good People use the Google Adwords API : 25 cents for 1000 units, 15++ units for keyword suggestion so they pay 4 or 5 dollar for 1000 keyword suggestions (if they can find a good programmer which also costs a few dollars). Or they opt for SemRush (also my preference), KeywordSpy, Spyfu, and other services like 7Search PPC programs to get keyword and traffic data and data on their competitors but these also charge about 80 dollars per month for a limited account up to a few hundred per month for seo companies. Good people pay plenty.

We tiny grey webmice of marketing however just want a few estimates, at low or better no cost : like this :

data num queries
google suggest 57800000
google suggestion box 5390000
google suggest api 5030000
google suggestion tool 3670000
google suggest a site 72700000
google suggested users 57000000
google suggestions funny 37400000
google suggest scraper 62800
google suggestions not working 87100000
google suggested user list 254000000

Suggestion autocomplete is AJAX, it outputs XML :

< ?xml version="1.0"? >
       <suggestion data="senior quotes"/>
       <num_queries int="30000000"/>
       <suggestion data="senior skip day lyrics"/>
       <num_queries int="441000"/>

Using SimpleXML, the PHP routine is as simple as querying g00gle.c0m/complete/search?, grabbing the autocomplete xml, and extracting the attribute data :

        if ($_SERVER['QUERY_STRING']=='') die('enter a query like http://host/filename.php?query');
	$contentstring = @file_get_contents("http://g00gle.c0m/complete/search?output=toolbar&q=".urlencode($kw));  
  	$content = simplexml_load_string($contentstring );

        foreach($content->CompleteSuggestion as $c) {
            $term = (string) $c->suggestion->attributes()->data;
            //note : traffic data is sometimes missing   
            $traffic = (string) $c->num_queries->attributes()->int;
            echo $term. " ".$traffic . "
" ;

I made a quick php script that outputs the terms as a list of new queries so you can walk through the suggestions :

The source is as text file up for download overhere (rename it to suggestit.php and it should run on any server with php5.* and simplexml).

php seo

bing api with php and simplexml

About scraping results off of Bing : Bing use a set of about eight cookies. You can grab 200 results with php curl, as 20 pages of 10, but after the first 200 the Bing server checks for the cookie and for lack of one returns a blank page. I can fidget with the curl cookiejar, but Bing also offer a straighforward API.

Using the Bing API to list search results is easier.

Bing TOS : not for seo rank checks

In the last paragraph of the api guide, Bing give a quick recap of their TOS, you can do max 7 queries per second, and using the results for SEO rank checks is explicitly prohibited.

These following snippets (text source) are hence explicitly not to be used for bing search engine result page (‘serp’) rank checks.

bing api with simplexml

So here is one for web results using php simplexml. The web api (which uses namespaces) allows for retrieving max 1000 results per term at max 50 results per query, you can specify the number of results and the offset, where to start grabbing results.

$Query = "seo rank check";
$Numres = 50; //max 50
$Offset = 1;    //up to 1000

$url = '

$feed = simplexml_load_file($url);
//use the web: namespace
 $children =  $feed->children('');
      foreach ($children->Web->Results->WebResult as $d) {
                echo $d->Title.'
'; echo $d->Description.'
'; echo $d->Url.'
'; echo $d->DisplayUrl.'
'; }

..and one for the pictures using php simplexml :

$Query = "alkmaar";
$Numres = 10;
$Offset = 1;

$url = '';
$url .= 'Appid='.$Appid;
$url .= '&query='.$Query;
$url .= '&sources=image';
$url .= '&image.count='.$Numres;
$url .= '&image.offset='.$Offset;

$feed = simplexml_load_file($url);

//use the mms: namespace       
  $children =  $feed->children('');

    '); foreach ($children->Image->Results->ImageResult as $d) { echo('
  • ' . $d->Title . '
    '); echo('
  • '); } echo("

I actually like that api, I am going to use that.

bing api with json

Bing seem to prefer you use json, less bandwidth usage. After their example in the api basics guide :

$Numres = 10;
$Offset = 1;

$url = '';
$url .= 'Appid='.$Appid;
$url .= '&query='.$Query;
$url .= '&sources=image';
$url .= '&image.count='.$Numres;
$url .= '&image.offset='.$Offset;

$response = file_get_contents($url);
$jsonobj = json_decode($response);
    '); foreach($jsonobj->SearchResponse->Image->Results as $value) { echo('
  • '); echo('
  • '); } echo("

Of course there is the old RSS-option, which doesnt require an appid but also falls under the api 2.0 tos, and a soap option.

other sources :
There is a bing api php class made over at routecafe, and a jquery bing plugin using json over at Einar Otto Stangvik’s blog.