2016-08-01 181 views
1

当我试图访问API MOZ使用以下代码Mozaps API请求被Incapsula阻止?

$accessID = 'mozscape-key'; 
$secretKey = 'secert key'; 
// 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)); 
// Specify the URL that you want link metrics for. 
$objectURL = "www.seomoz.org"; 
// 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 = "103079215108"; 
// Put it all together and you get your request URL. 
// This example uses the Mozscape URL Metrics API. 
$requestUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($objectURL)."?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$urlSafeSignature; 
echo $requestUrl; 
die; 
// Use Curl to send off your request. 
$options = array(
    CURLOPT_RETURNTRANSFER => true 
    ); 
$ch = curl_init($requestUrl); 
curl_setopt_array($ch, $options); 
$content = curl_exec($ch); 
curl_close($ch); 
$f = fopen('tte.txt','a'); 
fwrite($f,$content); 
fclose($f); 
print_r($content); 

的出它返回低于

<html style="height:100%"> 
<head> 
    <meta content="NOINDEX, NOFOLLOW" name="ROBOTS"> 
    <meta content="telephone=no" name="format-detection"> 
    <meta content="initial-scale=1.0" name="viewport"> 
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"> 
    <title></title> 
</head> 
<body style="margin:0px;height:100%"> 
    <iframe frameborder="0" height="100%" marginheight="0px" marginwidth="0px" 
    src="/_Incapsula_Resource?CWUDNSAI=9&xinfo=10-113037580-0%200NNN%20RT(1470041335360%200)%20q(0%20-1%20-1%20-1)%20r(0%20-1)%20B12(8,811001,0)%20U5&incident_id=220010400174850153-812164000562037002&edet=12&cinfo=08000000" 
    width="100%">Request unsuccessful. Incapsula incident ID: 
    220010400174850153-812164000562037002</iframe> 
    <meta content="NOINDEX, NOFOLLOW" name="ROBOTS"> 
    <meta content="telephone=no" name="format-detection"> 
    <meta content="initial-scale=1.0" name="viewport"> 
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"> 
    <iframe frameborder="0" height="100%" marginheight="0px" marginwidth="0px" 
    src="/_Incapsula_Resource?CWUDNSAI=9&xinfo=6-31536099-0%200NNN%20RT(1470041496215%200)%20q(0%20-1%20-1%20-1)%20r(0%20-1)%20B12(8,811001,0)%20U5&incident_id=220010400174850153-224923142338658566&edet=12&cinfo=08000000" 
    width="100%">Request unsuccessful. Incapsula incident ID: 
    220010400174850153-224923142338658566</iframe> 
</body> 
</html> 

好像incapsula被处理请求作为机器人。任何人都可以请帮助我如何修复它。

+0

更好的掩盖秘密密钥和accessID,如果它是私人的 –

+0

我做过之前,但后来又取代了代码。现在修复。谢谢 – deepsingh

+0

Basiaclly它说有一个请求的问题,“机器人”只适用于元数据,它不认为你是一个机器人。我不熟悉php,但请求参数应该是$ accessID或$ accessID(没有点),以及为什么要在URL之后添加objectURL? –

回答

0

如果你说你正在使用$ requestUrl到一个GET(在浏览器中)它工作正常,请尝试结合你的选项数组。

它应该是这样的:

$ch = curl_init(); 
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => $requestUrl, 
    CURLOPT_USERAGENT => 'Maybe You Need Agent?' 
)); 

备注剂(网络获取):

卷曲是一个庞然大物,而且有很多很多可能性。有些网站可能只向某些用户代理提供页面,并且在使用API​​时,某些 可能会要求您发送specfici用户代理,这可能是 意识到的问题。

另外值得一检查 - 你Incapsula有failture的ID - 220010400174850153-224923142338658566

您可以检查日志,看看还有什么?