Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何通过RESTAPI将商业订单从客户端存储发送到服务器/中心存储_Php_Woocommerce_Wordpress Rest Api_Restapi - Fatal编程技术网

Php 如何通过RESTAPI将商业订单从客户端存储发送到服务器/中心存储

Php 如何通过RESTAPI将商业订单从客户端存储发送到服务器/中心存储,php,woocommerce,wordpress-rest-api,restapi,Php,Woocommerce,Wordpress Rest Api,Restapi,我正在做一个实践,包括从客户机向服务器发送商业订单。一旦状态更改为“已完成”,客户端将通过RESTAPI发送订单,此时,服务器(或中央存储)将接收订单,并将其保存在自己的订单中。我已经为客户机和服务器编写了大部分代码,但是有些事情我不知道怎么做 事实上,我已经使用Postman软件进行了测试,以查看服务器侦听器是否正常工作 我对客户的怀疑是,我是否正确发送了整个订单 至于服务器,我不知道接收请求的函数中的代码是否正确:我指的是我使用的json_decode()以及是否必须首先使用json_enc

我正在做一个实践,包括从客户机向服务器发送商业订单。一旦状态更改为“已完成”,客户端将通过RESTAPI发送订单,此时,服务器(或中央存储)将接收订单,并将其保存在自己的订单中。我已经为客户机和服务器编写了大部分代码,但是有些事情我不知道怎么做

事实上,我已经使用Postman软件进行了测试,以查看服务器侦听器是否正常工作

我对客户的怀疑是,我是否正确发送了整个订单

至于服务器,我不知道接收请求的函数中的代码是否正确:我指的是我使用的json_decode()以及是否必须首先使用json_encode()

谢谢你的帮助

非常感谢

中央存储或服务器代码:

Plugin Name: Server
Plugin URI: https://www.misitio.com/
Version: 1.0
Author: Yo
Author URI: https://www.misitio.com/
License: GPL2

require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;

    $woocommerce = new Client(                                      
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx862df',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037',
        [

        'version' => 'wc/v3',
        ]
    );

       
    add_action('rest_api_init','registrar_mi_endpoint');

    function registrar_mi_endpoint() {
          register_rest_route( 'wc/v3', '/endpoint2/', array(
              'methods' => 'POST',
              'callback' => 'mi_consulta',
              'permission_callback' => function($request){
                  return current_user_can('manage_options');
              },
          ) );
    }

    function mi_consulta(WP_REST_Request $solicitud) {

        $pedido_procesado=json_decode($solicitud,true);
        
        $woocommerce->post('orders', $pedido_procesado);
   
    }
客户端代码

Plugin Name: Cliente
Plugin URI:  https://www.misitio.com/
Author: Yo
Author URI: https://www.misitio.com/
Version: 1.0
License: GPL2

require __DIR__ . '/vendor/autoload.php';

    use Automattic\WooCommerce\Client;
    use Automattic\WooCommerce\HttpClient\HttpClientException;

;
add_action('woocommerce_order_status_completed', 'funcion1');

function funcion1($order_id) {

    $woocommerce = new Client(
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9372',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdfcd',
        ['version' => 'wc/v3',]);

    $sales = $woocommerce->get('orders/'.$order_id);
    $var = json_encode($sales);
    
    $username ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx62df';
    $password ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037';

    
    $ch = curl_init("https://misitioservidor.com/wp-json/wc/v3/endpoint2/");

    
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

    
    curl_setopt($ch, CURLOPT_POST, TRUE);

    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $var);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if (!curl_errno($server_output)) {
        print_r('it works');
        print_r($status_code);
        exit;
    }else{
        print_r("it doesn't work");
        exit;
    }
}
Plugin Name: Cliente
Plugin URI:  https://www.misitio.com/
Author: Yo
Author URI: https://www.misitio.com/
Version: 1.0
License: GPL2

require __DIR__ . '/vendor/autoload.php';

    use Automattic\WooCommerce\Client;
    use Automattic\WooCommerce\HttpClient\HttpClientException;

;
add_action('woocommerce_order_status_completed', 'funcion1');

function funcion1($order_id) {

    $woocommerce = new Client(
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9372',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdfcd',
        ['version' => 'wc/v3',]);

    $sales = $woocommerce->get('orders/'.$order_id);
    $var = json_encode($sales);
    
    $username ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx62df';
    $password ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037';

    
    $ch = curl_init("https://misitioservidor.com/wp-json/wc/v3/endpoint2/");

    
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

    
    curl_setopt($ch, CURLOPT_POST, TRUE);

    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $var);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if (!curl_errno($server_output)) {
        print_r('it works');
        print_r($status_code);
        exit;
    }else{
        print_r("it doesn't work");
        exit;
    }
}

我已经能够为这两个插件编写代码。我把它们留在这里,以防对别人有帮助。我有一个具有更多功能的改进版

服务器代码:

Plugin Name: Server
Plugin URI: https://www.misitio.com/
Version: 1.0
Author: Yo
Author URI: https://www.misitio.com/
License: GPL2

require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;

    $woocommerce = new Client(                                      
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx862df',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037',
        [

        'version' => 'wc/v3',
        ]
    );

       
    add_action('rest_api_init','registrar_mi_endpoint');

    function registrar_mi_endpoint() {
          register_rest_route( 'wc/v3', '/endpoint2/', array(
              'methods' => 'POST',
              'callback' => 'mi_consulta',
              'permission_callback' => function($request){
                  return current_user_can('manage_options');
              },
          ) );
    }

    function mi_consulta(WP_REST_Request $solicitud) {

        $array = json_decode($solicitud->get_body());       
            
        $data2 = [                                          
            'payment_method' => $array->payment_method,
            'payment_method_title' => $array->payment_method_title,
            'billing' => [
                'first_name' => $array->billing->first_name,    
                'last_name' => $array->billing->last_name,      
                'address_1' => $array->billing->address_1,      
                'address_2' => '',
                'city' => $array->billing->city,                
                'state' => $array->billing->state,              
                'postcode' => $array->billing->postcode,        
                'country' => $array->billing->country,          
                'email' => $array->billing->email,              
                'phone' => $array->billing->phone               
            ],
            'shipping' => [
                'first_name' => $array->shipping->first_name,   
                'last_name' => $array->shipping->last_name,     
                'address_1' => $array->shipping->address_1,     
                'address_2' => '',
                'city' => $array->shipping->city,               
                'state' => $array->shipping->state,             
                'postcode' => $array->shipping->postcode,       
                'country' => $array->shipping->country          
            ],
            'line_items' => [
                [
                    'product_id' => $array->line_items[0]->product_id,
                    'quantity' => $array->line_items[0]->quantity
                ]
            ],
            'shipping_lines' => [
                [
                    'method_id' => $array->shipping_lines[0]->method_id,        
                    'method_title' => $array->shipping_lines[0]->method_title,  
                    'total' => $array->shipping_lines[0]->total                 
                ]
            ]
        ];  
            
        if($woocommerce->post('orders', $data2)){                                   
            print_r("PEDIDO INTRODUCIDO");
        }
   
    }
客户端代码:


我已经能够为这两个插件编写代码。我把它们留在这里,以防对别人有帮助。我有一个具有更多功能的改进版

服务器代码:

Plugin Name: Server
Plugin URI: https://www.misitio.com/
Version: 1.0
Author: Yo
Author URI: https://www.misitio.com/
License: GPL2

require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;

    $woocommerce = new Client(                                      
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx862df',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037',
        [

        'version' => 'wc/v3',
        ]
    );

       
    add_action('rest_api_init','registrar_mi_endpoint');

    function registrar_mi_endpoint() {
          register_rest_route( 'wc/v3', '/endpoint2/', array(
              'methods' => 'POST',
              'callback' => 'mi_consulta',
              'permission_callback' => function($request){
                  return current_user_can('manage_options');
              },
          ) );
    }

    function mi_consulta(WP_REST_Request $solicitud) {

        $array = json_decode($solicitud->get_body());       
            
        $data2 = [                                          
            'payment_method' => $array->payment_method,
            'payment_method_title' => $array->payment_method_title,
            'billing' => [
                'first_name' => $array->billing->first_name,    
                'last_name' => $array->billing->last_name,      
                'address_1' => $array->billing->address_1,      
                'address_2' => '',
                'city' => $array->billing->city,                
                'state' => $array->billing->state,              
                'postcode' => $array->billing->postcode,        
                'country' => $array->billing->country,          
                'email' => $array->billing->email,              
                'phone' => $array->billing->phone               
            ],
            'shipping' => [
                'first_name' => $array->shipping->first_name,   
                'last_name' => $array->shipping->last_name,     
                'address_1' => $array->shipping->address_1,     
                'address_2' => '',
                'city' => $array->shipping->city,               
                'state' => $array->shipping->state,             
                'postcode' => $array->shipping->postcode,       
                'country' => $array->shipping->country          
            ],
            'line_items' => [
                [
                    'product_id' => $array->line_items[0]->product_id,
                    'quantity' => $array->line_items[0]->quantity
                ]
            ],
            'shipping_lines' => [
                [
                    'method_id' => $array->shipping_lines[0]->method_id,        
                    'method_title' => $array->shipping_lines[0]->method_title,  
                    'total' => $array->shipping_lines[0]->total                 
                ]
            ]
        ];  
            
        if($woocommerce->post('orders', $data2)){                                   
            print_r("PEDIDO INTRODUCIDO");
        }
   
    }
客户端代码: