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

As discussed in the comments, here’s an attempt to fetch both pda and upa in a single request:

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

or a gist here. Note that:

  • I’ve copied your curl code since I assume it’s working fine, but there’s also wp_request_get you could use instead
  • I’m now testing the HTTP status code returned by curl and logging an error
  • I’m also checking that the upa or pda value you’ve extracted is numeric before trying to round it, which I think is where you’re going wrong: I’d guess you’re getting null values here rounded to 0 because you’re not noticing you’re getting errors from the API
  • the API you’re calling is out of date and there’s a new API you should be using instead
  • this version of the API also supports batched requests which you could use instead for all of your domains at once, rather than fetching them one at a time like this.