Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Batch Request URLs example in PHP

238 views
Skip to first unread message

Adam Lustig

unread,
Apr 9, 2011, 12:09:32 AM4/9/11
to SEOmoz API
I've been working on this for the past few hours and thought I'd
share. Here is an example of how to send multiple URLs to the SEOmoz
API using PHP. The wiki states you do this by "POSTing a JSON encoded
list of URLs in your request body." So the difference is you take out
the URL from your GET string, and put it as the cURL
CURLOPT_POSTFIELDS option. But you must do a json_encode() on your
array of URLs first. See below for working code.


<pre>
<?php
$objectURL = json_encode(array("www.youtube.com/watch?v=2jkExrrm_sQ",
"www.youtube.com/watch?v=O2FInaOCqoo"));
echo $objectURL;

$accessID = "YOUR ACCESS ID";
$secretKey = "YOUR SECRET KEY";
$expires = time() + 300;
$stringToSign = $accessID."\n".$expires;
$binarySignature = hash_hmac('sha1', $stringToSign, $secretKey,
true);
$urlSafeSignature = urlencode(base64_encode($binarySignature));
$urlToFetch = "http://lsapi.seomoz.com/linkscape/url-metrics/?
AccessID=".$accessID."&Expires=".$expires."&Signature=".
$urlSafeSignature;

$ch = curl_init($urlToFetch);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $objectURL);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
curl_close($ch);

$contents = json_decode($contents);

print("<pre>".print_r($contents, true)."</pre>");
?>
</pre>

katemats

unread,
May 9, 2011, 3:10:43 PM5/9/11
to SEOmoz API
This is a great example! Thanks so much for posting to the group! :)
Reply all
Reply to author
Forward
0 new messages