如何使用 shopify Post 新订单 API

How to Post new order using shopify API

我已经尝试使用来自 postman 或 ARC 的测试来创建新订单,并且成功了。但是,当我使用 php 代码执行操作时,我无法 Post 获得订单(例如,我可以获得产品)。

这是我的代码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);
    }
}
?>

我文件的一部分 post 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);
?>

正如我所说,我可以拿到产品但没有订单,也不能 post 订单。 我在 "read and write" 的私人应用程序中有订单。 预先感谢您的帮助。

根据您日志中的消息,您似乎没有订单实体的写入权限。

当您 ask users for permissions 时,在应用安装过程中,您需要指定您认为您的应用需要的所有权限。

授权 URL 看起来像:

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 文件中定义常量是不够的 - 安装了您的应用程序的商家需要授予您执行您想执行的操作的权限。

另请注意,您也无法更改已安装应用的这些权限。您必须先卸载该应用,确保您的代码请求正确的权限,然后使用新权限重新安装该应用。

您会在应用授权屏幕上看到您的应用请求哪些权限。

希望对您有所帮助!