且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Wordpress wp_remote_post

更新时间:2023-11-30 23:25:04

您需要在php.ini文件中启用cURL。



wp_remote_post()使用一个名为 WP_Http 的类,传输类(见文件 class-http.php function _get_first_available_transport )。



POST方法将使用类 WP_Http_Curl ,但不能使用类 WP_Http_Streams )。



另一种方法是使用 wp_remote_get()


I want to use wp_remote_post to send information from one server to my other website.

So basically, I have added this line to my code -

$sidebarHTTP = site_url(); // Retrieves HTTP Url of sidebar
$sidebarActivation = $sidebar.' , '.$sidebarHTTP; // Activate Sidebar 
$args = array(
  'method' => 'post',
  'body' => array('sidebar' => $sidebar, 'sidebarHTTP' => $sidebarHTTP),
  'user-agent' => 'My site'
);
wp_remote_post( 'http://mysite.com', $args ); // Loads all default data   

So basically, it doesn't send anything. Yes, I have correct domain entered. Maybe it does send something, but I don't know how can I retrieve the $args['body'] from that site. Also, I tried adding $response = wp_remote_post.... and then sending mail $response['body'], but it just sends source code of homepage to email.

Would appreciate help.

You will need to enable cURL in your php.ini file.

wp_remote_post() uses a class called WP_Http that in turn can use one of three transport classes (see file class-http.php function _get_first_available_transport).

POST method will work with class WP_Http_Curl, but will not work with class WP_Http_Streams (the cURL fallback).

The alternative is to use wp_remote_get()