我尝试使用邮递员或ARC的测试创建新订单,这是有效的。但是当我从我的PHP代码中做到时,我不能发布任何订单(我可以获得产品)。
这是我的代码Class.Shopify.php
<?php
public function call($method, $path, $params=array())
{
$baseurl = "https://{$this->shop_domain}/";
//var_dump($this->shop_domain);
$url = $baseurl.ltrim($path, '/');
$query = in_array($method, array('GET','DELETE')) ? $params : array();
$payload = in_array($method, array('POST','PUT')) ? json_encode($params) : array();
$request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
// add auth headers
$request_headers[] = 'X-Shopify-Access-Token: ' . $this->token;
$response = $this->curlHttpApiRequest($method, $url, $query, $payload, $request_headers);
//var_dump($response);
$response = json_decode($response, true);
if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response);
return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
}
private function curlHttpApiRequest($method, $url, $query='', $payload='', $request_headers=array())
{
$url = $this->curlAppendQuery($url, $query);
//echo $url.' '.$payload;
$ch = curl_init($url);
/*var_dump($ch);
var_dump($method);
var_dump($payload);
var_dump($request_headers);*/
$this->curlSetopts($ch, $method, $payload, $request_headers);
$response = curl_exec($ch);
//var_dump($response);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno) throw new ShopifyCurlException($error, $errno);
list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
$this->last_response_headers = $this->curlParseHeaders($message_headers);
return $message_body;
}
private function curlAppendQuery($url, $query)
{
if (empty($query)) return $url;
if (is_array($query)) return "$url?".http_build_query($query);
else return "$url?$query";
}
private function curlSetopts($ch, $method, $payload, $request_headers)
{
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
/*WHEN CODE DEPLOY TO LIVE SERVER, CURLOPT_SSL_VERIFYPEER MUST BE TRUE*/
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, 'ohShopify-php-api-client');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($request_headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
if ($method != 'GET' && !empty($payload))
{
if (is_array($payload)) $payload = http_build_query($payload);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload);
}
}
?>
发布order.php的文件的一部分
$credential = $qdb->fetch();
//var_dump($credential);
//$id = $_REQUEST['id'];
$mail = $credential['mail'];
$url = $credential['site'];
$key = $credential['key'];
$token = $credential['token'];
$country = $credential['name'];
$id_country = $credential['id_country'];
ob_start();
//where Shopify api key and shopify secret are the credentials for the orders private app
$shop = new ShopifyClient($url, $credential['token'], SHOPIFY_API_KEY, SHOPIFY_SECRET);
$orderData = array('order' => array(
'line_items' => array(
array(
'variant_id' => 30781726924,
'quantity' => 1
)
)));
$product = $shop->call('POST', "/admin/orders.json", $orderData);
var_dump($product);
$content = ob_get_contents();
file_put_contents('update-pr.txt', $content);
?>
正如我所说,我可以获得产品,但没有订单,也无法发布订单。 我在私人应用中订购了“读写”。 在此先感谢您的帮助。
答案 0 :(得分:3)
根据日志中的消息,您似乎没有订单实体的写入权限。
当您ask users for permissions时,在应用安装过程中,您需要指定您认为应用所需的所有权限。
授权网址如下所示:
https://{shop}.myshopify.com/admin/oauth/authorize?client_id={api_key}&scope={scopes}&redirect_uri={redirect_uri}&state={nonce}&grant_options[]={option}
在您的情况下,范围将是“read_products,write_products,read_orders,write_orders”
仅仅在PHP文件中定义常量是不够的 - 商家在您的商店中安装了您的应用程序,需要授予您执行您想要执行的操作的权限。
另请注意,您也无法在已安装的应用上更改这些权限。您必须首先卸载该应用程序,确保您的代码要求正确的权限,然后使用新权限重新安装该应用程序。
您会在应用授权屏幕上看到您的应用要求的权限。
希望这有帮助!