php - 使用变体和图像 Shopify API PHP 添加产品
问题描述
我的阵列有什么问题?我正在尝试使用 API 在 shopify 上添加产品。但它不会添加产品的价格和图片。
这是示例数组:
Array
(
[product] => Array
(
[title] => TITLE
[body_html] => <p><strong>DESCRIPTION</strong></p>
[vendor] => TESTSTORE
[product_type] =>
[tags] =>
[published] => 1
[variants] => Array
(
[0] => Array
(
[price] => 1160
)
)
[images] => Array
(
[0] => Array
(
[src] => urlofimage.jpg
)
[1] => Array
(
[src] => urlofimage.jpg
)
)
)
)
这是我调用以将其添加到shopify的代码:
function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {
// Build URL
$url = "https://" . $shop . $api_endpoint;
if (!is_null($query) && in_array($method, array('GET', 'DELETE'))) $url = $url . "?" . http_build_query($query);
// Configure cURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
// curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
// Setup headers
$request_headers[] = "";
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) {
if (is_array($query)) $query = http_build_query($query);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
}
// Send request to Shopify and capture any errors
$response = curl_exec($curl);
$error_number = curl_errno($curl);
$error_message = curl_error($curl);
// Close cURL to be nice
curl_close($curl);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// No error, return Shopify's response by parsing out the body and the headers
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
// Convert headers into an array
$headers = array();
$header_data = explode("\n",$response[0]);
$headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
array_shift($header_data); // Remove status, we've already set it above
foreach($header_data as $part) {
$h = explode(":", $part);
$headers[trim($h[0])] = trim($h[1]);
}
// Return headers and Shopify's response
return array('headers' => $headers, 'response' => $response[1]);
}
}
上面的数组是 $query 变量的值。你认为是什么问题?
解决方案
我正在检查同样的一切都很好请检查您的令牌和端点
请更新您的图片网址,例如:https ://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201809210816
你可以试试我的代码它可能对你有帮助
<?php
$params = [];
$params['product'] = [
'title'=>'TITLEcus',
'body_html'=>'<p><strong>DESCRIPTION</strong></p>',
'vendor'=>'TESTSTORE',
'product_type'=>'',
'tags' =>'' ,
'published'=>1,
'variants'=> [
[
'price'=>1160
]
],
'images'=> [
[
'src'=>'urlofimage.jpg',
],
[
'src'=>'urlofimage.jpg',
]
]
];
function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {
$url = "https://" . $shop . $api_endpoint;
if (!is_null($query) && in_array($method, array('GET', 'DELETE'))) $url = $url . "?" . http_build_query($query);
// Configure cURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
// curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_USERAGENT, 'ohShopify-php-api-client');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
// Setup headers
$request_headers[] = "";
$query = in_array($method, array('POST','PUT')) ? json_encode($query) : array();
$request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) {
if (is_array($query)) $query = http_build_query($query);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
}
// Send request to Shopify and capture any errors
$response = curl_exec($curl);
$error_number = curl_errno($curl);
$error_message = curl_error($curl);
// Close cURL to be nice
curl_close($curl);
// Return an error is cURL has a problem
if ($error_number) {
return $error_message;
} else {
// No error, return Shopify's response by parsing out the body and the headers
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
// Convert headers into an array
$headers = array();
$header_data = explode("\n",$response[0]);
$headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
array_shift($header_data); // Remove status, we've already set it above
foreach($header_data as $part) {
$h = explode(":", $part);
$headers[trim($h[0])] = trim($h[1]);
}
// Return headers and Shopify's response
return array('headers' => $headers, 'response' => $response[1]);
}
}
var_dump(shopify_call($token, $shop, '/admin/api/2019-07/products.json', $params, 'POST', $request_headers = array()));
?>
推荐阅读
- sql - Oracle 将 CLOB 中的字符串替换为 sql 作为值
- sql - SQL中的多列到不同的行
- android - 使用 WRITE_EXTERNAL_STORAGE 在 Android 中将位图存储为 jpg
- excel - Excel VBA 现在有一个新列,但随后它停止了
- android - 相对布局中的文本视图和按钮在 android oreo 和 pie 中不可见,但在其他 android 版本中完美可见
- java - 什么时候使用嵌套的 Java 类真正有用?
- php - 在 PHP 中使用 foreach 和 for 循环的问题
- python - 在python的不同列中的列内拆分字典列表
- google-app-engine - Google Cloud Build 无法触发构建:没有这样的 blob
- ios - iOS微信SDK登录集成问题:系统错误。错误代码:10026