Wordpress 不发送带有数据的 POST 请求

时间:2021-03-04 17:38:13

标签: wordpress api curl http-post

我需要使用 POST 请求将 woocommerce 订单数据导出到第三方 url。我在 functions.php 中有此代码

add_action('woocommerce_thankyou', 'wdm_send_order_to_ext'); 
function wdm_send_order_to_ext ( $order_id ){
   // get order object and order details
   $order = new WC_Order( $order_id ); 
   $email = $order->get_billing_email();
   $phone = $order->get_billing_phone();
   $shipping_type = $order->get_shipping_method();
   $shipping_cost = $order->get_total_shipping();

   // set the address fields
   $user_id = $order->get_user_id();
   $address_fields = array('country',
       'title',
       'first_name',
       'last_name',
       'company',
       'address_1',
       'address_2',
       'address_3',
       'address_4',
       'city',
       'state',
       'postcode');

   $address = array();
   if(is_array($address_fields)){
       foreach($address_fields as $field){
           $address['billing_'.$field] = get_user_meta( $user_id, 'billing_'.$field, true );
           $address['shipping_'.$field] = get_user_meta( $user_id, 'shipping_'.$field, true );
       }
   }
   
   // set the username and password
   $api_username = 'username';
   $api_password = 'password';

       // setup the data which has to be sent
   $data = array(
           'username' => $api_username,
           'password' => $api_password,
       'name1' => $address['billing_first_name'],
           'street' => $address['billing_address_1'],
           'city' => $address['billing_city'],
       'country' => $address['billing_country'],
           'pcode' => $address['billing_postcode'],
           'phone' => $phone,
           'weight' => 4.0,
       'parcel_type' => 'D',
       'num_of_parcel' => 1
       );

           // send API request via cURL
       $curl = curl_init();

       /* set the complete URL, to process the order on the external system. Let’s consider http://example.com/buyitem.php is the URL, which invokes the API */
       curl_setopt($curl, CURLOPT_URL, "https://easyship.si/api/parcel/parcel_import");
       curl_setopt($curl, CURLOPT_POST, 1);
       curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   
       $response = curl_exec ($curl);
   
       curl_close ($curl);
       
}

但是,它没有通过请求,第三方 API 没有收到任何东西。 WP_DEBUG 没有看到任何错误。告诉我在哪里看,有什么问题?

1 个答案:

答案 0 :(得分:0)

您可以使用 https://requestbin.com/ 作为端点来查看您发布的数据以及它们是否正确发布。如果它成功将其发布到 requestbin 端点,您还可以看到标头和正文格式。如果它不起作用,请尝试逐行调试。 抱歉,我无法将此添加为评论