我正在尝试设置一个短代码来获取Moz API数据,但我面临着一些问题。有时,它会将值返回到0,有时它会工作。我不知道我做错了什么。可能我需要删除现有瞬态,或者检查瞬态中的值是否为0。此外,我使用相同的代码通过使用不同的瞬态键重写此函数来检索不同的MOZ API数据。我能把这段代码合并成一段吗?就像[moz\\u score\\u da]和[moz\\u score\\u pa]一样,这里是代码:主要的问题是,我被一些返回零的瞬态值所困扰,然后我必须手动删除它们,它会拉回正确的值。
我在ACF字段中使用此短代码。如果可能,请指导我是否可以使用瞬态和短代码直接更新acf字段。
function post_title_shortcode(){
return get_the_title();
}
add_shortcode(\'post_title\',\'post_title_shortcode\');
function moz_score_shortcode($atts) {
extract(
shortcode_atts(
array(
\'domain\' => get_the_title(),
),
$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 = "mozscape-####"; // * Add unique Access ID
$secretKey = "####"; // * Add unique Secret Key
$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, (60*60*72) );
}
return $seo_grade;
}
add_shortcode( \'moz_score\',\'moz_score_shortcode\' );
第二个代码是
function moz_pa_shortcode($atts) {
extract(
shortcode_atts(
array(
\'domain\' => get_the_title(),
),
$atts
)
);
if ( ! $domain )
return; // No domain, nothing to return
$cache_key_pa = \'agency_moz_pa_\' . $domain;
if ( false === $seop_grade = get_transient( $cache_key_pa ) ) {
// Setting Moz API connection
$accessID = "mozscape-#####"; // * Add unique Access ID
$secretKey = "######"; // * Add unique Secret Key
$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=103079215140&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);
$seop_grade = $resObj->upa;
$seop_grade = round($seop_grade, 0);
set_transient( $cache_key_pa, $seop_grade, (60*60*72) );
}
return $seop_grade;
}
add_shortcode( \'moz_pa\',\'moz_pa_shortcode\' );
最合适的回答,由SO网友:Rup 整理而成
正如评论中所讨论的,这里尝试在一个请求中获取pda和upa:
/**
* Make a Moz v1 URL Metrics request for a given domain
*
* @see https://moz.com/help/links-api/v1-archive/v1-url-metrics
* @see https://moz.com/help/links-api/v1-archive/response-fields
* @param $domain
*/
function moz_v1_links_api_request($domain) {
if ( !$domain ) {
return NULL;
}
$cache_key = \'agency_moz_url_metrics_\' . $domain;
$url_metrics = get_transient( $cache_key );
if ( false === $url_metrics ) {
// Setting Moz API connection
$accessID = "mozscape-####"; // * Add unique Access ID
$secretKey = "####"; // * Add unique Secret Key
$expires = time() + 300;
$SignInStr = $accessID. "\\n" .$expires;
$binarySignature = hash_hmac(\'sha1\', $SignInStr, $secretKey, true);
$SafeSignature = urlencode(base64_encode($binarySignature));
// Connecting to Moz API url
// 103079215140 = 0x1800000024, the flags for pda, upa, ueid and uu respectively
$reqUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($domain)."?Cols=103079215140&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);
$status_code = curl_getinfo($curlhandle, CURLINFO_HTTP_CODE);
curl_close($curlhandle);
if ( $status_code < 200 || $status_code >= 300 ) {
// HTTP request failed
error_log(\'moz_v1_links_api_request for \' . $domain . \' failed: \' . $status_code . \' \' . print_r( $content, true) );
// Cache an empty object for 30 seconds so we retry shortly
$url_metrics = new stdClass();
set_transient( $cache_key, $url_metrics, 30 );
} else {
// Cache the object returned for three days
$url_metrics = json_decode($content);
set_transient( $cache_key, $url_metrics, (60*60*72) );
}
}
return $url_metrics;
}
function moz_score_shortcode($atts) {
extract(
shortcode_atts(
array(
\'domain\' => get_the_title(),
),
$atts
)
);
if ( ! $domain )
return NULL; // No domain, nothing to return
$url_metrics = moz_v1_links_api_request( $domain );
// Getting \'pda\' from Moz API and then rounding
if ( isset( $url_metrics->pda ) ) {
$seo_grade = $url_metrics->pda;
if (is_numeric($seo_grade)) {
$seo_grade = round($seo_grade, 0);
}
} else {
// No value returned
$seo_grade = NULL;
}
return $seo_grade;
}
add_shortcode( \'moz_score\',\'moz_score_shortcode\' );
function moz_pa_shortcode($atts) {
extract(
shortcode_atts(
array(
\'domain\' => get_the_title(),
),
$atts
)
);
if ( ! $domain )
return NULL; // No domain, nothing to return
$url_metrics = moz_v1_links_api_request( $domain );
// Getting \'upa\' from Moz API and then rounding
if ( isset( $url_metrics->upa ) ) {
$seop_grade = $url_metrics->upa;
if ( is_numeric( $seop_grade ) ) {
$seop_grade = round($seop_grade, 0);
}
} else {
// No value returned
$seop_grade = NULL;
}
return $seop_grade;
}
add_shortcode( \'moz_pa\',\'moz_pa_shortcode\' );
或a
gist here. 请注意:
我已经复制了你的curl代码,因为我认为它工作正常,但还有一个wp\\u request\\u get你可以使用,我现在正在测试curl返回的HTTP状态代码并记录错误,我还在检查你提取的upa或pda值是否是数字,然后再尝试对其进行取整,我想这就是你错的地方:我猜这里的空值被舍入为0,因为你没有注意到你从API中得到了错误。你调用的API已经过时了,你应该使用一个新的API来代替它。这个版本的API还支持batched requests 您可以一次将其用于所有域,而不是像这样一次只获取一个域