shopify 创建客户api调用报错, customer:expected String to be a Hash

shopify里面创建一个客户的api定义是这样的:

POST /admin/customers.json
{
  "customer": {
    "email": "test@test.com",
    "first_name": "test first",
    "last_name": "test name"
  }
}

php里面模拟post提交创建客户:

	$customer = [
		"email"      => "test@test.com", 
		"first_name" => "test first",
		"last_name"  => "test last"
	];

	$data = ['customer' => $customer];

	$url = "https://{your_api}:{your_secret}@{yourshop}.myshopify.com/admin/customers.json";
	  
	$ret = post($url, $data);
	exit($ret);
  
  
  /**
     * 模拟post提交
     *
     * @param     $url
     * @param     $post_data
     * @param int $timeout
     *
     * @return mixed|string
     */
    function post($url, $post_data, $timeout = 5)
    { 
        $ch = curl_init();
 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        //if($post_data != ''){
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        // }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    

        $file_contents = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }

        curl_close($ch);
        return $file_contents;
    }

结果报错:

"{\"errors\":{\"customer\":\"expected String to be a Hash\"}}"

原来是模拟post时要添加json的httpheader,同时post_data要是json字符串格式的:

   $data = '{
          "customer": {
            "email": "test@test.com",
            "first_name": "test first",
            "last_name": "test last"
          }
        }';

	$url = "https://{your_api}:{your_secret}@{yourshop}.myshopify.com/admin/customers.json";
	  
	$ret = post($url, $data);
	exit($ret);
	
    /**
     * 模拟post提交
     *
     * @param     $url
     * @param     $post_data
     * @param int $timeout
     *
     * @return mixed|string
     */
    function post($url, $post_data, $timeout = 5)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        if($post_data != ''){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        $header = array();
        $header[] = 'Content-Type:application/json;charset=utf-8';
        $header[] = 'Content-Length: ' . strlen($post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        $file_contents = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }

        curl_close($ch);
        return $file_contents;
    }
ok了,返回结果:
"{\"customer\":{\"id\":397395165230,\"email\":\"test@test.com\",\"accepts_marketing\":false,\"created_at\":\"2018-02-28T11:21:14+08:00\",\"updated_at\":\"2018-02-28T11:21:14+08:00\",\"first_name\":\"test first\",\"last_name\":\"test last\",\"orders_count\":0,\"state\":\"disabled\",\"total_spent\":\"0.00\",\"last_order_id\":null,\"note\":null,\"verified_email\":true,\"multipass_identifier\":null,\"tax_exempt\":false,\"phone\":null,\"tags\":\"\",\"last_order_name\":null,\"addresses\":[]}}"

 

发一个可以模拟get/post的函数:

      /**
         * get\post方式的curl函数
         * 
         * @param  string  $url    地址
         * @param  string  $method 请求方式post/get
         * @param  string  $data   数据
         * @return [type]          [description]
         */
        public static function httpRequest($url, $method = 'GET', $data = ''){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            if ($method != 'GET'){
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8','Content-Length: ' . strlen($data)));
            }
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL,$url);
            $result = curl_exec($ch);
            curl_close($ch);
            return $result;
        }

再发一个模拟delete的:

  /**
     * 模拟delete提交
     *
     * @param     $url
     * @param     $post_data
     * @param int $timeout
     *
     * @return mixed|string
     */
    function delete($url, $post_data, $timeout = 5)
    { 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_URL, $url);

        if($post_data != ''){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOSIGNAL, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

        $file_contents = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }

        curl_close($ch);
        return $file_contents;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值