我是个新来的过渡者,一直很难切换。我能够修复脸书和推特API的短代码,但我无法让Moz API作为一个过渡版本工作。
下面是正在工作的短代码,下面是我尝试在它不再工作的瞬态中进行编码(没有显示任何内容,没有错误)。如果这也有帮助的话,最终的结果就是Moz的域授权(pda)api端点,这只是数字。
如果有人能指出我在哪里搞乱了瞬态,我将不胜感激。
// Working version, no transient
function moz_score_shortcode($atts) {
extract(shortcode_atts(array(
\'domain\' => \'\',
), $atts));
// Setting Moz API connection
$accessID = "{my api name is here}";
$secretKey = "{my api password is here}";
$expires = time() + 300;
$SignInStr = $accessID. "\\n" .$expires;
$binarySignature = hash_hmac(\'sha1\', $SignInStr, $secretKey, true);
$SafeSignature = urlencode(base64_encode($binarySignature));
// Connecting to Moz API url
$reqUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($domain)."?Cols=103079215108&AccessID=".$accessID."&Expires=".$expires."&Signature=".$SafeSignature;
// Send request with curl
$opts = array(
CURLOPT_RETURNTRANSFER => true
);
$curlhandle = curl_init($reqUrl);
curl_setopt_array($curlhandle, $opts);
$content = curl_exec($curlhandle);
curl_close($curlhandle);
// Getting \'pda\' from Moz API and then rounding
$resObj = json_decode($content);
$seo_grade = $resObj->{\'pda\'};
$seo_grade = round($seo_grade, 0);
return $seo_grade;
}
add_shortcode(\'moz_score\',\'moz_score_shortcode\');
我在这里的尝试是:
// Transient version, not working, displaying nothing
function moz_score_shortcode($atts) {
extract(shortcode_atts(array(
\'domain\' => \'\',
), $atts));
// If no domain then return data
if ($domain == "NULL") {
return $seo_grade;
} else {
$moz_key = \'agency_moz_score_\' . $domain;
$seo_grade = get_transient($moz_key);
// If no transient start processing transient
if ( false === ( $pda = get_transient($moz_key) ) ) {
// Setting Moz API connection
$accessID = "{my api name is here}";
$secretKey = "{my api password is here}";
$expires = time() + 999;
$SignInStr = $accessID. "\\n" .$expires;
$binarySignature = hash_hmac(\'sha1\', $SignInStr, $secretKey, true);
$SafeSignature = urlencode(base64_encode($binarySignature));
// Connecting to Moz API url
$requestUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($domain)."?Cols=103079215108&AccessID=".$accessID."&Expires=".$expires."&Signature=".$SafeSignature;
// Send request with curl
$opts = array(
CURLOPT_RETURNTRANSFER => true
);
$curlhandle = curl_init($reqUrl);
curl_setopt_array($curlhandle, $opts);
$content = curl_exec($curlhandle);
curl_close($curlhandle);
// Setting transient key
set_transient( $moz_key, $pda, 60*60);
// Getting \'pda\' from Moz API and then rounding
$resObj = json_decode($content);
$seo_grade = $resObj->{\'pda\'};
$seo_grade = round($seo_grade, 0);
return $seo_grade;
}
}
}
add_shortcode(\'moz_score\',\'moz_score_shortcode\');
最合适的回答,由SO网友:TheDeadMedic 整理而成
你需要回来$seo_grade
外部if
i、 e.在函数的最后——此时,你在说“如果没有瞬态,创建一个并返回它,否则什么也不返回”。
同样在顶部,您返回$seo_grade
如果域是"NULL"
, 但是该变量将是未定义的(可能的输入错误-您正在检查的域是一个名为“NULL”的字符串,而不是实际的NULL)。
function moz_score_shortcode($atts) {
extract(
shortcode_atts(
array(
\'domain\' => \'\',
),
$atts
)
);
if ( ! $domain )
return; // No domain, nothing to return
$cache_key = \'agency_moz_score_\' . $domain;
if ( false === $seo_grade = get_transient( $cache_key ) ) {
// Setting Moz API connection
$accessID = "{my api name is here}";
$secretKey = "{my api password is here}";
$expires = time() + 300;
$SignInStr = $accessID. "\\n" .$expires;
$binarySignature = hash_hmac(\'sha1\', $SignInStr, $secretKey, true);
$SafeSignature = urlencode(base64_encode($binarySignature));
// Connecting to Moz API url
$reqUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($domain)."?Cols=103079215108&AccessID=".$accessID."&Expires=".$expires."&Signature=".$SafeSignature;
// Send request with curl
$opts = array(
CURLOPT_RETURNTRANSFER => true
);
$curlhandle = curl_init($reqUrl);
curl_setopt_array($curlhandle, $opts);
$content = curl_exec($curlhandle);
curl_close($curlhandle);
// Getting \'pda\' from Moz API and then rounding
$resObj = json_decode($content);
$seo_grade = $resObj->pda;
$seo_grade = round($seo_grade, 0);
set_transient( $cache_key, $seo_grade );
}
return $seo_grade;
}
add_shortcode( \'moz_score\',\'moz_score_shortcode\' );