Robert hue
Robert hue

Reputation: 302

Trying to get this PHP code to work for bulk action

This is the code in question. I got this from moz website.

This code connects with Moz API and let us check domain authority/page authority in bulk. It works fine but the issue is that it allows maximum of 200 URL in a go.

So I am trying to make multiple requests on API if there are more than 200 domains. I had done this to check 400 domains by creating 2 API requests in code. Here is modified code.

<?php
    // Get your access id and secret key here: https://moz.com/products/api/keys
    $accessID = "ACCESS_ID_HERE";
    $secretKey = "SECRET_KEY_HERE";

    // Set your expires times for several minutes into the future.
    // An expires time excessively far in the future will not be honored by the Mozscape API.
    $expires = time() + 300;

    // Put each parameter on a new line.
    $stringToSign = $accessID."\n".$expires;

    // Get the "raw" or binary output of the hmac hash.
    $binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true);

    // Base64-encode it and then url-encode that.
    $urlSafeSignature = urlencode(base64_encode($binarySignature));

    // Add up all the bit flags you want returned.
    // Learn more here: https://moz.com/help/guides/moz-api/mozscape/api-reference/url-metrics
    $cols = "68719476736";

    // Put it all together and you get your request URL.
    $requestUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$urlSafeSignature;

    // Put your URLS into an array and json_encode them.
    $batchedDomains = array('www.moz.com', 'www.apple.com', 'www.pizza.com');
    $batchedDomains1 = array_slice($batchedDomains, 0, 200);
    $batchedDomains2 = array_slice($batchedDomains, 200, 200);

    $encodedDomains1 = json_encode($batchedDomains1);
    $encodedDomains2 = json_encode($batchedDomains2);

    $options1 = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POSTFIELDS     => $encodedDomains1
    );
    $options2 = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POSTFIELDS     => $encodedDomains2
    );

    $ch1 = curl_init( $requestUrl );
    curl_setopt_array( $ch1, $options1 );
    $content1 = curl_exec( $ch1 );
    curl_close( $ch1 );

    $ch2 = curl_init( $requestUrl );
    curl_setopt_array( $ch2, $options2 );
    $content2 = curl_exec( $ch2 );
    curl_close( $ch2 );

    $contents1 = json_decode( $content1 );
    $contents2 = json_decode( $content2 );

    $contents = json_decode($content);
    print_r($contents1);
    print_r($contents2);

?>

This works for 2 requests but if I had to check 1000 or may be 2000 domains then I will have to create 20 blocks of this code to process all domains.

I am looking for a way to automate this process. If there are more than 200 domains to check, code will automatically create batches of 200 (by split array) and make API requests for each batch and then combine the results again.

Upvotes: 0

Views: 1168

Answers (1)

Alex Andrei
Alex Andrei

Reputation: 7283

If you store the domains in database table you can create a function that fetches all the rows and returns an array.

Assuming you get an array of more than 200 elements called $domains

$domains = array(
        'www.moz.com',
        'www.apple.com',
        'www.pizza.com'
        // ...
    );

Using array_chunk we split it into chunks of 200 elements, and we call a function that sends a request to the moz api for each of these chunks.
The last chunk may contain less than 200 elements.

$chunks = array_chunk($domains,200);

foreach($chunks as $chunk){
    $response = sendRequest(json_encode($chunk));
    var_dump($response);
}

function sendRequest($jsonString){
    // Get your access id and secret key here: https://moz.com/products/api/keys
    $accessID = "ACCESS_ID_HERE";
    $secretKey = "SECRET_KEY_HERE";

    // Set your expires times for several minutes into the future.
    // An expires time excessively far in the future will not be honored by the Mozscape API.
    $expires = time() + 300;

    // Put each parameter on a new line.
    $stringToSign = $accessID."\n".$expires;

    // Get the "raw" or binary output of the hmac hash.
    $binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true);

    // Base64-encode it and then url-encode that.
    $urlSafeSignature = urlencode(base64_encode($binarySignature));

    // Add up all the bit flags you want returned.
    // Learn more here: https://moz.com/help/guides/moz-api/mozscape/api-reference/url-metrics
    $cols = "68719476736";

    // Put it all together and you get your request URL.
    $requestUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$urlSafeSignature;

    $options = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POSTFIELDS     => $jsonString
    );

    $ch = curl_init( $requestUrl );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    curl_close( $ch );

    return json_decode($content);
}

You can collect the $response variables in an array and use them later.

The code in the sendRequest function is perfectly recyclable, we don't need to rewrite it over and over, just pass a parameter to the CURLOPT_POSTFIELDS

Upvotes: 3

Related Questions