# \[HTTP API] wp\_remote\_post (ok)

#### Ví dụ cơ bản 1

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\wpremotepost.php

```
<?php  
	echo 'ggggggggg';
?>
```

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPNbSYAI2g-UiwLCwTq%2F-MPNeJsd00Jgf1pq3ZR2%2FScreenshot_5.jpg?alt=media\&token=04e24236-27a8-4f47-a482-7fe4f0b8a2d7)

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\functions.php

```
$url = get_template_directory_uri().'/wpremotepost.php';
$response = wp_remote_post(
    $url,
    array(
		'body'    => array(
		    'unique-id'   => 'unique-id',
		    'address'     => 'address',
		    'page-viewed' => 'page-viewed'
		)
	)
);
echo '<pre>';
	var_export($response);
echo '</pre>';
```

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPNbSYAI2g-UiwLCwTq%2F-MPNeZC2TPX3uFCih93E%2FScreenshot_6.jpg?alt=media\&token=41fcc3d2-4747-4ef4-a9c9-d104236dd228)

#### Ví dụ cơ bản 2

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPNbSYAI2g-UiwLCwTq%2F-MPNf4O1HaXFA2wntZyR%2FScreenshot_7.jpg?alt=media\&token=49f9fc3a-054e-4394-9448-36d38c78d1b4)

C:\xampp\htdocs\wordpress\wp-content\themes\twentytwentyone\wpremotepost.php

```
<?php  
	echo '<pre>';
		var_export($_POST);
	echo '</pre>';
?>
```

#### Ví dụ hoàn thành Ví dụ cơ bản 1, Ví dụ cơ bản 2

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPN6xFDnihC4HqLBr-z%2F-MPNaVs7dBD9HZwKUH5M%2FScreenshot_1.jpg?alt=media\&token=c98940f0-0025-4d8e-8524-5eb0b7b6784a)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPN6xFDnihC4HqLBr-z%2F-MPNaYxixonQqKPdz4LX%2FScreenshot_2.jpg?alt=media\&token=55b61d56-f3d0-425f-801b-268ef6f742bf)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPN6xFDnihC4HqLBr-z%2F-MPNaaPl5KwE-uRValcw%2FScreenshot_3.jpg?alt=media\&token=96e3ed48-2a2f-4d67-a007-faa7ebb2ee26)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPN6xFDnihC4HqLBr-z%2F-MPNadAcik5FU_jGTrtX%2FScreenshot_4.jpg?alt=media\&token=dc8b2614-3c77-430f-91f1-6b6a36601af8)

C:\xampp\htdocs\wordpress\wp-content\plugins\wp-remote-post-example\class-wp-remote-post.php

```
<?php
/**
 * WP Remote Post Example
 *
 * An example plugin demonstrating how to use `wp_remote_post`.
 *
 * @package   WP_Remote_Post
 * @author    Tom McFarlin <tom@tommcfarlin.com>
 * @license   GPL-2.0+
 * @link      http://tommcfarlin.com
 * @copyright 2013 Tom McFarlin
 */
/**
 * WP Remote Post Example
 *
 * An example plugin demonstrating how to use `wp_remote_post`.
 *
 * @package   WP_Remote_Post
 * @author    Tom McFarlin <tom@tommcfarlin.com>
 */
class WP_Remote_Post_Example {
	/**
	 * Instance of this class.
	 *
	 * @since    1.0.0
	 *
	 * @var      object
	 */
	protected static $instance = null;
	/**
	 * Initialize the plugin by setting localization, filters, and administration functions.
	 *
	 * @since     1.0.0
	 */
	private function __construct() {
		add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'add_style_sheet' ) );
		add_action( 'the_content', array( $this, 'get_post_response' ) );
	} // end __construct
	/**
	 * Return an instance of this class.
	 *
	 * @since     1.0.0
	 *
	 * @return    object    A single instance of this class.
	 */
	public static function get_instance() {
		if ( null == self::$instance ) {
			self::$instance = new self;
		}
		return self::$instance;
	} // end get_instance
	/**
	 * Load the plugin text domain for translation.
	 *
	 * @since    1.0.0
	 */
	public function load_plugin_textdomain() {
		load_plugin_textdomain( 'wprp-example', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
	} // end load_plugin_textdomain
	/**
	 * Add adds the plugin style sheet to the single post page.
	 *
	 * @since     1.0.0
	 */
	public function add_style_sheet() {
		if( is_single() ) {
			wp_enqueue_style( 'wp-remote-post-example-style', plugins_url( 'wp-remote-post-example/css/display.css' ) );
		} // end if
	} // end add_style_seet
	/**
	 * Appends the post response after making a request to the receiver to the content.
	 *
	 * @param     string    $content    The post content.
	 * @return    string    $content    The modified content included the post information.
	 * @since     1.0.0
	 */
	public function get_post_response( $content ) {
		// If we're on a single page...
		if( is_single() ) {
			// Go ahead and set the ID, site URL, and permalink for this page to variables
			$unique_id = $_SERVER['REMOTE_ADDR'];
			$site_url = site_url();
			$page_url = get_permalink();
			// Grab the URL to the remote receiver file
			$url = plugins_url('wp-remote-post-example/wp-remote-receiver.php');
			// Make the remote request and retrieve ther esponse
			$response = wp_remote_post(
			    $url,
			    array(
					'body'    => array(
					    'unique-id'   => $unique_id,
					    'address'     => $site_url,
					    'page-viewed' => $page_url
					)
				)
			);
			// If there's an error, display a message
			if ( is_wp_error( $response ) ) {
				$html = '<div id="post-error">';
					$html .= __( 'There was a problem retrieving the response from the server.', 'wprp-example' );
				$html .= '</div><!-- /#post-error -->';
			// Otherwise, display the data and save the post meta data
			} else {
				$html = '<div id="post-success">';
					$html .= '<p>' . __( 'Your message posted with success! The response was as follows:', 'wprp-example' ) . '</p>';
					$html .= '<p id="response-data">' . $response['body'] . '</p>';
				$html .= '</div><!-- /#post-error -->';
				$this->save_post_data( $unique_id, $site_url, $page_url );
			} // end if/else
			$content .= $html;
		} // end if
		return $content;
	} // end get_post_response
	/**
	 * Appends the post response after making a request to the receiver to the content.
	 *
	 * @param     string    $unique_id  The post content.
	 * @param     string    $site_url   The post content.
	 * @param     string    $page_url   The post content.
	 * @since     1.0.0
	 */
	private function save_post_data( $unique_id, $site_url, $page_url ) {
		if ( '' == get_post_meta( get_the_ID(), 'unique_id', true ) ) {
			add_post_meta( get_the_ID(), 'unique_id', $unique_id );
			add_post_meta( get_the_ID(), 'site_url', $site_url );
			add_post_meta( get_the_ID(), 'page_url', $page_url );
		} // end if
	} // end save_post_data
} // end class
```

C:\xampp\htdocs\wordpress\wp-content\plugins\wp-remote-post-example\wp-remote-receiver.php

```
<?php
/**
 * WP Remote Post Receiver
 *
 * A file that retrieves values from a POST request and then renders
 * the markup for the request.
 *
 * @package   WP_Remote_Post
 * @author    Tom McFarlin <tom@tommcfarlin.com>
 * @license   GPL-2.0+
 * @link      http://tommcfarlin.com
 * @copyright 2013 Tom McFarlin
 */
// Build up the HTML display of of the data
$html = '<div id="wp-remote-post-example-container">';
	$html .= '<h4>The Post Data</h4>';
	$html .= '<ul>';
	foreach( $_POST as $key => $value ) {
		$html .= '<li>' . $key . ': ' . $value . '</li>';
	} // end foreach
	$html .= '</ul>';
$html .= '</div><!-- /#wp-remote-post-example-container -->';
// Finally, echo the HTML to the requester
echo $html;
```

C:\xampp\htdocs\wordpress\wp-content\plugins\wp-remote-post-example\wp-remote-post.php

```
<?php
/**
 * WP Remote Post Example
 *
 * An example plugin demonstrating how to use `wp_remote_post`.
 *
 * @package   WP_Remote_Post
 * @author    Tom McFarlin <tom@tommcfarlin.com>
 * @license   GPL-2.0+
 * @link      http://tommcfarlin.com
 * @copyright 2013 Tom McFarlin
 *
 * @wordpress-plugin
 * Plugin Name: WP Remote Post Example
 * Plugin URI:  http://tommcfarlin.com/wp-remote-post/
 * Description: An example plugin demonstrating how to use `wp_remote_post`.
 * Version:     1.0.0
 * Author:      Tom McFarlin
 * Author URI:  http://tommcfarlin.com
 * Text Domain: wprp-locale
 * License:     GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * Domain Path: /lang
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
} // end if

require_once( plugin_dir_path( __FILE__ ) . 'class-wp-remote-post.php' );
WP_Remote_Post_Example::get_instance();
```

Đây là toàn bộ source&#x20;

{% file src="https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPNahmoXOPT-piJvsXW%2F-MPNbQdMVNzPO9RigoEa%2Fwp-remote-post-example.zip?alt=media&token=be26ea6a-d031-47d6-afd2-70ffb422670a" %}
