简体   繁体   中英

How to use CURL to send data for Wordpress

I would like to send data from one WP site to an other. I have two plugins the sender ad the reciver. In the sender side I used the init hook

add_action('init', array($this, 'say_hello') );

....

public function say_hello() {
$data = get_plugin_data( $this->pDIR.'/plugin.php', $markup = false, $translate = false );

$url = $_SERVER['HTTP_HOST'].'/wp-admin/admin-ajax.php';

$fields = array(
          'action' => 'Hello_ajax',
          'zpm_hello' => '1',
          'zpm_plugin_version' => $data['Version'],
          'zpm_host' => $_SERVER['HTTP_HOST']
);

foreach($fields as $key=>$value) { 
        $fields_string .= $key.'='.$value.'&'; 
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
var_dump($result);
//close connection
curl_close($ch);

}

The variables are fine the url is correct I printed out. On the reciver side I registered the admin ajax

add_action('wp_ajax_Hello_ajax', array($this, 'zpm_api_hello'));
add_action('wp_ajax_nopriv_Hello_ajax', array($this, 'zpm_api_hello'));

public function zpm_api_hello() {
    $response['success'] = false;
    $response['response'] = "";
    $error = false;

    $hello          = $_POST['zpm_hello'];
    $plugin_version = $_POST['zpm_plugin_version'];
    $plugin_host    = $_POST['zpm_host'];

    if(!isset($hello)) {
        $error = true;
        $response['response'] .= "Ello >> Auth ERROR - CODE - 01 ";
    } 

    if(!isset($plugin_version)) {
        $error = true;
        $response['response'] .= "Ello >> Plugin Version error - CODE - 02 ";
    }

    if(!isset($plugin_host)) {
        $error = true;
        $response['response'] .= "Ello >> Plugin host error - CODE - 03 ";
    }


    if(!$error) {
        $response['success'] = true;
        $response['response'] .= "Ello >> Auth OK ";
    }

    echo json_encode($response);
    die();
}

So the code is here. My problem is when I try to load the sender site it is just loading and loading for ever and I doesn't get back the result. Could you tell me what could be the problem with this method? Thanks all of your answers!

you might want to use this example to build your PHP request, if you send me the POST body, headers and url parameters I will even build it for you:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://example.com//wp-admin/admin-ajax.php",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "blakey=blaval",
  CURLOPT_HTTPHEADER => array(
    "action: Hello_ajax",
    "cache-control: no-cache",
    "content-type: application/x-www-form-urlencoded",
    "postman-token: fd5bcc0b-f46d-0cf8-a5cf-dc83bfe7dbec",
    "zpm_hello: 1"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM