0

I'm new to transients and have been having a tough time switching over. I was able to fix my shortcodes for Facebook and Twitter API, but I can't get Moz API to work as a transient.

Below is the working shortcode, underneath that is my attempt at coding in transients where it no longer works (displays nothing, no error). In case it helps too, the end result is Moz's Domain Authority (pda) api endpoint, which is just numbers.

Appreciate any help if someone can point out where I'm messing up with transients.

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

And my attempt here:

// 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');
1
  • Is the key being saved to the database? Or is the problem on display?
    – s_ha_dum
    Nov 4, 2015 at 13:46

1 Answer 1

0

You need to return $seo_grade outside the if i.e. at the very end of your function - at the moment you're saying "if no transient, create one and return it, otherwise return nothing".

Also at the top, you return $seo_grade if the domain is "NULL", but the variable will be undefined (possible typo - you're checking domain is a string called "NULL", not actually 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' );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.