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
Well, we could explode by space
" "
, then by;
Ie. Live on phpsandbox
You need to properly explode the new-line characters in order to get to the
csv
structure, then parse it, ascsv
Using the sample response from https://www.semrush.com/api-analytics/#phrase_organic,
the above will output
The
if
statement is there to filter out the first line, the csv header.