• Hi, I’m new to php and tries to print out an external api to the wordpress-page with shortcode in the plugin that I created.
    But the code I have is not working, what is wrong? Here is the code:

    add_shortcode('external_data', 'callback_function_name');
    
    function callback_function_name() {
        
        $url = 'https://api.fortnox.se/3/articles/';
        $args = array(
            'headers' => array(
              'Content-Type' => 'application/json',
              'Accept' => 'application/json',
              'Access-Token' => 'accessToken',
              'Client-Secret' => 'ClientSecret'
            )
            );
    
            $response = wp_remote_get( $url, $args );
    
            if (is_wp_error($response)) {
                $error_message = $response->get_error_message();
                return "Something went wrong: $error_message";
            }
    
            $results = json_decode(wp_remote_retrieve_body($response));
    
        
            foreach($results as $result) {
            $html += "<p>" . $result->Articles->Description . "</p>";
            }
    
            return $html;
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You are using the maths addition/assignment operator +=. In PHP this is invalid for strings, you must use the concatenation/assignment operator .=.

    Thread Starter susanna85

    (@susanna85)

    Thank you for your answer but it didn’t work unfortunately.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use external api to print data via shortcode’ is closed to new replies.