0

I would like to make a wordpress plugin that will display summarized information from a table stored in the wordpress database. My question is how do I process the HTTP POST from the remote service using wordpress methods?

I was looking for a tutorial that would illuminate this process.

I cold just use straight php/mysql and store the info directly to the table, but I would like to do it "correctly" using Wordpress to sanitize the data and keep project under the wordpress plugin.

1 Answer 1

1

you can simply use wp_remote_post:

$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>';
}

And after you receive the data and perform any required modifications, you can insert them into custom table using $wpdb:

global $wpdb;
$wpdb->insert( 
    'table', 
    array( 
        'column1' => 'value1', 
        'column2' => 123 
    ), 
    array( 
        '%s', 
        '%d' 
    ) 
);

this will require creating a custom table, which can be created like that:

global $wpdb;    
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     $custom_table = $wpdb->prefix . 'custom_table_name';
        $sql = "CREATE TABLE IF NOT EXISTS $custom_table (
    id mediumint(9) NOT NULL,
    col1 varchar(50) NOT NULL,
    col2 varchar(500) NOT NULL,
    ) $charset_collate;";

        dbDelta($sql);

refer to wp_remote_post, $wpdb and wp_insert_post in WordPress documentation for more information

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think that wp_remote_post will help. The remote server/web service sends the data real-time to a URL of my choice. I need to create a service to receive that information. But thank you anyway.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.