This is the code有问题。我从moz网站上得到了这个。
此代码与Moz API连接,让我们批量检查域权限/页面权限。它工作正常,但问题是它允许最多200个URL。
因此,如果有超过200个域,我会尝试在API上发出多个请求。我这样做是为了通过在代码中创建2个API请求来检查400个域。这是修改后的代码。
<?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);
?>
这适用于2个请求,但如果我必须检查1000个或可能是2000个域,那么我将不得不创建20个此代码块来处理所有域。
我正在寻找一种自动化此过程的方法。如果要检查的域名超过200个,代码将自动创建200个批次(通过拆分数组)并为每个批次生成API请求,然后再次合并结果。
答案 0 :(得分:2)
如果将域存储在数据库表中,则可以创建一个获取所有行并返回数组的函数。
假设您获得了一个名为$domains
$domains = array(
'www.moz.com',
'www.apple.com',
'www.pizza.com'
// ...
);
使用array_chunk
我们将其分成200个元素的块,我们调用一个函数向moz api发送每个块的请求。
最后一个块可能包含少于200个元素。
$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);
}
您可以收集数组中的$response
变量,稍后再使用它们。
sendRequest
函数中的代码完全可循环使用,我们不需要反复重写,只需将参数传递给CURLOPT_POSTFIELDS