Php 如何从Wordpress自定义端点内部初始化POST和PUT方法

Php 如何从Wordpress自定义端点内部初始化POST和PUT方法,php,wordpress,api,woocommerce,Php,Wordpress,Api,Woocommerce,我正在wordpress中创建一个自定义端点,该端点应执行以下操作: 1.创建路径(完成) 2.调用函数,该函数: 2.1. 从WooCommerce获取所有产品并将其存储在数组中(完成) 2.2. 将WooCommerce上的产品与外部数据库中的产品进行比较,如果数据存在任何差异,则通过外部数据库中的数据进行更新(PUT) 2.3. 如果某些产品存在于外部数据库中,但不存在于WooCommerce中,请在WooCommerce上创建它们(POST) 我已经设法通过wc_get_products

我正在wordpress中创建一个自定义端点,该端点应执行以下操作: 1.创建路径(完成) 2.调用函数,该函数: 2.1. 从WooCommerce获取所有产品并将其存储在数组中(完成) 2.2. 将WooCommerce上的产品与外部数据库中的产品进行比较,如果数据存在任何差异,则通过外部数据库中的数据进行更新(PUT) 2.3. 如果某些产品存在于外部数据库中,但不存在于WooCommerce中,请在WooCommerce上创建它们(POST)

我已经设法通过wc_get_products($args)从WooCommerce获取所有数据,但我找不到如何编写PUT和POST方法来更新或创建产品。 如果我使用Automatic/WooCommerce从独立文件(不是从自定义端点)推送或放置产品,但如果我将Automatic/WooCommerce保留在自定义端点上,它会告诉我以下信息:

Failed to connect to localhost port 8000: Connection refused in /var/www/html/wp-content/plugins/wl-api/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 417 
这是完全有意义的,因为从自定义端点我已经连接到WooCommerce,并且自动/WooCommerce将尝试再次连接。除了PUT和PUSH之外,还有像wc_get_产品这样的方法吗

//CUSTOM ENDPOINT PHP FILE
add_action('rest_api_init', 'customEndpoint');

function customEndpoint() {
    register_rest_route('endpoint', '/transfer', array(
        'methods' => 'GET',
        'callback' => 'get_data',
    ));
}
function get_data() {
    include "updateOrCreate.php";
}

//updateOrCreate.php
require 'vendor/autoload.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require 'vendor/guzzlehttp/guzzle/src/Client.php';
use Automattic\WooCommerce;
//-----------Connect to the External DB----------
$baseUrl=...;
$externalClient = new GuzzleHttp\Client();
$res = $externalClient->request('GET', $baseUrl, [
    "query" => ['fields' => $fields, 'aktiv' => true],
    'auth' => ['...', '...']
]);
$articles = json_decode($res->getBody());

//-----------Connect to WooCommerce DB
$wooKey= "...";
$wooSecret= "...";
$wooCommerceBaseUrl = "http://localhost:8000";
$wooCommerceClient = new WooCommerce\Client($wooCommerceBaseUrl,
$wooKey, $wooSecret,
[
    'wp_api' => true,
    'version' => 'wc/v3',
]
);
//GET all products from WooCommerce
$args = array(
'status' => 'publish',
'limit' => -1,
);  
$wooExistingProducts = wc_get_products($args);



$productsForWoo= [];

//check if there are no products on woocommerce:
if(empty($wooExistingProducts)){
foreach ($articles as &$article){
//Create the necessary template to initialize the connections between external products and woocommerce product template
    $wooTemplate = [
...
    ];
    array_push($productsForWoo, $wooTemplate);
//CREATE BATCHES OF 10
    if(sizeof($productsForWoo) == 10){
        $data = [
            'create' => $productsForWoo,
        ];
        $wooCommerceClient->post('products/batch', $data);
        $productsForWoo = [];
    }
}
//As there is a big chance that the last batch will have less than 10 products, push them as well to wooCommerce as a last batch
    $data = [
        'create' => $productsForWoo,
    ];
    $wooCommerceClient->post('products/batch', $data);
}
// if there are existing products on woocommerce
else{
//Loop through all existing products on the external DB
    foreach ($articles as &$article){ 
            $did_match = false;
            $wooTemplate = [
                ...
            ];
//loop through all products on WooCommerce
            foreach ($wooExistingProducts as &$product){
//if the product id from the external db matches the id from an existing product on woocommerce
                if(...){
//update that product
            $wooCommerceClient->put('products/'.urlencode($product->id), $wooTemplate);
            $did_match = true;
            break;
        }
    }
//otherwise, if the product from the external db is not found on the woocommerce database, create it
    if(!$did_match){
    array_push($productsForWoo, $wooTemplate);
    if(sizeof($productsForWoo) == 10){
        $data = [
            'create' => $productsForWoo,
        ];
        $wooCommerceClient->post('products/batch', $data);
        $productsForWoo = [];
    }
}
}
//As there is a big chance that the last batch will have less than 10 products, push them as well to wooCommerce as a last batch
            $data = [
            'create' => $productsForWoo,
            ];
            $wooCommerceClient->post('products/batch', $data);
}

我将非常感谢与我的问题有关的任何意见。谢谢大家!

要获得POST和PUT,您需要实现这些方法(动词)

您可以使用:

$verb = $_SERVER['REQUEST_METHOD'];
echo $verb;
。。。测试您正在使用的HTTP请求方法(PUT vs GET)

下面是一个我已将其添加为书签并在排除故障时经常使用的示例。这里提供的代码非常干净,是一个很好的起点