gpt4 book ai didi

php - 如何使用 CURL 为 Wordpress 发送数据

转载 作者:行者123 更新时间:2023-12-05 07:52:23 25 4
gpt4 key购买 nike

我想将数据从一个 WP 站点发送到另一个站点。我有两个插件发送者和接收者。在发件人端我使用了 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);

}

变量很好,我打印出来的 url 是正确的。在接收端我注册了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();
}

所以代码在这里。我的问题是,当我尝试加载发件人网站时,它一直在加载和加载,我没有取回结果。你能告诉我这种方法可能有什么问题吗?感谢您的所有回答!

最佳答案

您可能想使用此示例来构建您的 PHP 请求,如果您向我发送 POST 正文、 header 和 url 参数,我什至会为您构建它:

<?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;
}

关于php - 如何使用 CURL 为 Wordpress 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com