【问题标题】:How to Post new order using shopify API如何使用 shopify API 发布新订单
【发布时间】:2017-03-01 21:22:43
【问题描述】:

我尝试使用邮递员或 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);
?>

正如我所说,我可以得到产品但没有订单,也不能发布订单。 我在“读写”的私人应用程序中有订单。 提前感谢您的帮助。

【问题讨论】:

  • 嗨!而你的调用方法没有报错?你收到的回复怎么样?里面有什么?
  • 不,我检查了我的日志,当我发布帖子时,文件 update-pr.txt 没有更新,但是通过获取产品,我收到了文件中的所有产品。我不知道如何将响应错误捕获为同一文件 txt 中的输出。
  • 但是我看到你在 cal 方法中注释掉了var_dump($response);...当它没有被注释时它输出了什么?
  • 我忘记了。让我再检查一下。
  • 我收到这个 "{"errors":"[API] 这个动作需要 write_orders 范围。"}" 但我还没有定义('SHOPIFY_SCOPE','write_orders');在文件中

标签: php api shopify


【解决方案1】:

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

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

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

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

希望这会有所帮助!

【讨论】:

  • 你是对的。我有一个疑问,我怎样才能获得私人应用程序密码?我试过使用$password = $this-&gt;is_private_app ? $this-&gt;secret : md5($this-&gt;secret.$this-&gt;token); $baseurl = "https://{$this-&gt;api_key}:$password@{$this-&gt;shop_domain}/";,但我无法找回我在私人应用程序中的密码,而且密码不是。
  • 我使用$this-&gt;secret,这是私人应用程序密码。我打印了$url 输出,与我在邮递员中使用的输出相同,但从插件中我仍然收到此错误 [API] Invalid API key or access token (unrecognized login or wrong password)
  • 抱歉,我不确定我是否理解您的问题...如果您查看您的ShopifyClient 课程,其中应该有一个生成授权网址的方法。这应该是你开始的地方......
  • 这是令牌,我已经发送了包含标题中令牌的调用并在邮递员中进行了测试我已经看到我应该在没有令牌的情况下发送它并且它的工作。非常感谢@MacPrawn。
  • 太棒了!很高兴我能帮上忙!
猜你喜欢
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多