Shopify API - 更新客户的详细信息 - 错误:缺少必需参数

时间:2014-01-14 10:01:04

标签: php json curl shopify

美好的一天,

我正在尝试更新现有客户,但我总是收到错误:缺少必需参数

我的代码遗漏了什么或者有什么问题?

感谢您的帮助。

以下是我的参考:http://docs.shopify.com/api/customer#update

代码:

<?php

    $data = array ('customer' => 
              array (
                'accepts_marketing' => false,
                'created_at' => '2014-01-14T12:22:35+08:00',
                'email' => $_POST['email'],
                'first_name' => $_POST['first_name'],
                'id' => $_POST['id'],
                'last_name' => $_POST['last_name'],
                'last_order_id' => null,
                'multipass_identifier' => null,
                'note' => null,
                'orders_count' => 0,
                'state' => 'enabled',
                'total_spent' => '0.00',
                'updated_at' => '2014-01-14T12:30:29+08:00',
                'verified_email' => true,
                'tags' => 'newsletter, prospect',
                'last_order_name' => null,
                'default_address' => 
                array (
                  'address1' => '123 ABC',
                  'address2' => '456 DEF',
                  'city' => 'Sydney',
                  'company' => '',
                  'country' => 'Australia',
                  'first_name' => $_POST['first_name'],
                  'id' => $_POST['id'],
                  'last_name' => $_POST['last_name'],
                  'phone' => '1231231',
                  'province' => null,
                  'zip' => '2000',
                  'name' => $_POST['first_name'].' '.$_POST['last_name'],
                  'province_code' => null,
                  'country_code' => 'AU',
                  'country_name' => 'Australia',
                  'default' => true,
                ),
                'addresses' => 
                array (
                  0 => 
                  array (
                    'address1' => '123 ABC',
                    'address2' => '456 DEF',
                    'city' => 'Sydney',
                    'company' => '',
                    'country' => 'Australia',
                    'first_name' => $_POST['first_name'],
                    'id' => $_POST['id'],
                    'last_name' => $_POST['last_name'],
                    'phone' => '1231231',
                    'province' => null,
                    'zip' => '2000',
                    'name' => $_POST['first_name'].' '.$_POST['last_name'],
                    'province_code' => null,
                    'country_code' => 'AU',
                    'country_name' => 'Australia',
                    'default' => true,
                  ),
                ),
                "metafields" =>
                array (
                  0 => 
                  array (
                    'key' => 'contact_number',
                    'value' => $_POST['contact_number'],
                    'value_type' => 'string',
                    'namespace' => 'global'
                  ),
                  1 =>
                  array (
                    'key' => 'date_of_birth',
                    'value' => $_POST['date_of_birth'],
                    'value_type' => 'string',
                    'namespace' => 'global'
                  ),
                ),
              ),
            );



        $id = $_POST['id'];

        $url = 'https://' . $API_KEY . ':' . $PASSWORD . '@' . $STORE_URL . '/admin/customers/'.$id.'.json';

        $session = curl_init( $url );

        curl_setopt($session, CURLOPT_HEADER, false);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($fields));

        //curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($session, CURLOPT_PUT, true);

        if(ereg("^(https)",$url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);

        $response = curl_exec($session);
        curl_close($session);

        $json = json_decode( $response, true );

        var_dump($json);

?>

再次感谢。

2 个答案:

答案 0 :(得分:0)

首先,您已声明变量$data = array ('customer' =>并使用变量$fields发布,请在代码中检查此行:

curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($fields));

但这不是问题。我猜主要的问题是JSON。您已将请求标头设置为'Content-Type: application/json',但发送了正常的HTTP发布数据。所以你的数据应该是:

curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));

答案 1 :(得分:0)

您错过了在标题中定义预期的Content-Type。 Shopify支持多种类型,因此需要知道您期望的数据类型。

Content-Type : application/json