未登录时创建wp_remote_post

时间:2015-12-17 10:57:30

标签: php ajax wordpress curl woocommerce

您好我正在开发一个插件,为订单添加新字段(WooCommerce)。该字段需要对我的插件中的文件发出ajax请求,然后该文件需要向另一个网站(或wp_remote_post)发出cURL请求。但是在提出请求时我遇到了困难。

我无法使用普通的cURL,也无法使用wp_remote_post函数。

这是我的文件中ajax请求的cURL片段。

<?php

$shipping_place = array(
                'country_code' => $country_code,
                'postcode' => $postcode,
                'street' => $street,
                'number_of_droppoints' => $number_of_droppoints
            );

            $auth = array(
                'Content-Type: application/json',
                'Authorization: Basic '. base64_encode('user:password')
            );

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $auth);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $shipping_place);
            $result = curl_exec($ch);

            if(curl_errno($ch)){
                $msg = 'Curl error: ' . curl_error($ch);
            } else {
                $result = json_decode($result['body']);

                if ( $result->status == 'error' ) {
                    echo $result;
                }

                pred($result);

                echo $result->result;
            }

            curl_close ($ch);

&GT;

1 个答案:

答案 0 :(得分:1)

解决:我必须本地化wp-admin脚本。

那会像这样使用wp函数

<?php wp_localize_script( $handle, $name, $data ); ?> 

参考here