phpwordpress

How to Integrate 3rd party API in Wordpress


I'm using wordpress and i want to integrate an SMS API into my wordpress site. Can anyone help in knowing where (in which file) to write the code for integration and also the code to integrate SMS API.

My SMS API Url is : http://www.elitbuzzsms.com/app/smsapi/index.php?key=KEY&campaign=****&routeid=**&type=text&contacts=< NUMBER >&senderid=SMSMSG&msg=< Message Content >

I want to integrate above API in my wordpress theme so that i can send sms based on mobile number and add required message.


Solution

  • In wordpress you can use wp_remote_get and wp_remote_post

    get request example

    $url = 'http://www.elitbuzzsms.com/app/smsapi/index.php?key=KEY&campaign=****&routeid=**&type=text&contacts=< NUMBER >&senderid=SMSMSG&msg=< Message Content >';
    
    $response = wp_remote_get( $url  );
    if( is_array($response) ) {
      $header = $response['headers']; // array of http header lines
      $body = $response['body']; // use the content
    }
    

    post request example

    $response = wp_remote_post( $url, array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'headers' => array(),
        'body' => array( 'username' => 'bob', 'password' => '1234xyz' ),
        '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>';
    }