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;
}