skip to Main Content

I’m trying the REST API here: https://www.semrush.com/api-analytics/ , specifically the Organic Results, but no matter what I’ve tried, I can’t seem to manipulate the data. Can someone tell me how to do this? I’ve tried SimpleXML, JSON, and even breaking up the response via explode() but I must be missing something because all I can do is push the result to the beginning of an array and not actually break it up.

This is my current code:

    $url = "http://api.semrush.com/?type=phrase_organic&key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&display_limit=10&export_columns=Dn,Ur&phrase=seo&database=us";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    var_dump($result);

With the result being:

string 'Domain;Url
site-analyzer.com;https://www.site-analyzer.com/
woorank.com;https://www.woorank.com/
hubspot.com;http://blog.hubspot.com/blog/tabid/6307/bid/33164/6-SEO-Tools-to-Analyze-Your-Site-Like-Google-Does.aspx
seoworkers.com;http://www.seoworkers.com/tools/analyzer.html
seositecheckup.com;http://seositecheckup.com/
site-seo-analysis.com;http://www.site-seo-analysis.com/
webseoanalytics.com;http://www.webseoanalytics.com/free/seo-tools/web-seo-analysis.php
seocentro.com;http://www.seocentro.com/t'... (length=665)

Is there a simple way to break this up so I can manipulate or reformat the response?

2

Answers


  1. Well, we could explode by space " ", then by ;

    $response = explode(" ", trim(str_replace("Domain;Url", "", $response)));
    
    $readableResponse = [];
    
    foreach($response as $r)
    {
      $e = explode(";", $r);
      $readableResponse[$e[0]] = $e[1];
    }
    
    print_r($readableResponse);
    

    Ie. Live on phpsandbox

    [searchengineland.com] => http://searchengineland.com/guide/what-is-seo
    [wikipedia.org] => https://en.wikipedia.org/wiki/Search_engine_optimization
    ....
    
    Login or Signup to reply.
  2. You need to properly explode the new-line characters in order to get to the csv structure, then parse it, as csv

    foreach(preg_split("/((r?n)|(rn?))/", $response) as $key=>$line){
    
        if ($key!=0) {
            list($domain,$url) = str_getcsv($line,';');
            print 'Domain: ' . $domain . ', URL: ' . $url . PHP_EOL;
        }
    
    }
    

    Using the sample response from https://www.semrush.com/api-analytics/#phrase_organic,
    the above will output

    Domain: wikipedia.org, URL: http://en.wikipedia.org/wiki/Search_engine_optimization
    Domain: searchengineland.com, URL: http://searchengineland.com/guide/what-is-seo
    Domain: moz.com, URL: http://moz.com/beginners-guide-to-seo
    

    The if statement is there to filter out the first line, the csv header.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search