Need help in setting up a transient using API Key and if possible updating it in the ACF field

Question

I am trying to set up a shortcode that will fetch Moz API data but I am facing issues. Sometimes, it returns values to 0, and sometimes it works. I don’t know what I am doing wrong here. Might be I need to delete the existing transients or to check if value = 0 in transients. Further, I am using the same code for retrieving different MOZ API data by rewriting this function using different transient keys. Can I merge this code into one? Like [moz_score_da] and [moz_score_pa] Here is the code: The major issue is that I am stuck with some transient values returning zero and then I have to delete them manually and it pulls back the correct value.

I am using this shortcode in the ACF field. If possible, kindly guide me If I can directly update the acf field using transients and shortcodes.

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' );

The second code is

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' );
0
, , , ali 7 months 0 Answers 93 views 0

Leave an answer