0

What terminology can I use to help me different between using Wordpress to access a 3rd party API (what I want to do) and building an API for a 3rd party to use (not what I want to do)?

Explanation:

I'm interested in writing some custom code to access a 3rd party API (a bespoke product) and use the results to add or update some custom post data in my Wordpress site.

I've searched the web for information on this, and had a little success, but I wonder if I'm missing a lot because I don't understand the terminology. Most the search results I'm getting relate to creating an API layer in Wordpress for 3rd party software to connect to, but that's not what I'm interested in.

I am aware of the Wordpress HTTP API documentation which seems to indicate the wp_remote_* functions are going to be useful, but it would still be very useful if I could more easily find some example code and possibly some helpful plugins.

1
  • did what i sent work for you?
    – rudtek
    Commented Sep 9, 2016 at 15:49

1 Answer 1

2

Wp_remote is on the right path. Here's a portion of a plugin I wrote for newsletter signups to a third party. Basically you're taking advantage of THAT company's api, not using wordpress'. The main problem is that many companies have api's that are not similar, so without knowing more about the company you're interacting with you'll have to do a little testing or see if you can get an exposed result from them.

function html_form_code() {  // THIS IS THE FORM TO SEND COLLECT USER DATA TO SEND TO 3rd party.
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
    echo '<p>';
    echo 'Your Name (required) <br />';
    echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p>';
    echo 'Your Email (required) <br />';
    echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
    echo '</p>';
    echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
    echo '</form>';
}

function newsletter_signup() {  //THIS IS THE API INTERACTION.

    // if the submit button is clicked, send the POST
    if ( isset( $_POST['cf-submitted'] ) ) {

        // sanitize form values
        $name    = sanitize_text_field( $_POST["cf-name"] );
        $email   = sanitize_email( $_POST["cf-email"] );


$url='YOUR API URL WILL GO HERE'; //THIS is the actual interaction so this is where you are going to have to do some testing.
$response = wp_remote_post( $url, array(
    'method' => 'POST',
    'timeout' => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking' => true,
    'headers' => array('Authorization' => 'Basic ' . base64_encode(MY REMOVED PW)),
    'body' => array(    ),  // YOU'll NEED TO ADD YOUR CONTENT YOU'RE sending here... //$name and $email in my example but formatting will be up to API
    'cookies' => array()
    )
);

if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   echo "Something went wrong: $error_message";
} else {
 //  echo 'Response:<pre>';
 //  print_r( $response );
 //    echo '</pre>'; 
$responseBody = json_decode($response['body'],true);
echo $responseBody['message'];

    }
    }
}

function cf_shortcode() {  //FUNCTION to combine and submit the data in a fell swoop
    ob_start();
    newsletter_signup();
    html_form_code();
    return ob_get_clean();
}

add_shortcode( 'newsletter_contact_form', 'cf_shortcode' ); //SHORTCODE TO PRESENT ON FRONTEND.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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