【发布时间】: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');在文件中