Shopify API - 使用 PHP 上传图片

Shopify API - Upload Image with PHP

我正在尝试通过 API 使用 PHP 将图片上传到我的 shopify 商店,但失败了。

$images = array
        (
            "image"=>array
            (
                "attachment" => $borderedImage,
                "filename" => "rails_logo.jpg"
            )
        );

        # Making an API request can throw an exception
         $products = $shopify('POST /admin/products/#1246990273/images.json', $images);

但是,我的错误日志中没有得到 NULL,而是以下消息

[2015-07-17 23:14:57] local.INFO: <html><body>You are being <a href="https://SHOPNAME.myshopify.com/admin/auth/login">redirected</a>.</body></html>  

我认为这是我的访问令牌或 api 密钥的问题,但我可以进行其他 API 调用以获取产品列表和创建新产品。它只是创建一个会产生问题的新图像。

很难获得任何信息,因为似乎没有多少人在为 shopify 构建 PHP 应用程序。

有没有其他人使用 PHP 应用程序成功将图像上传到 api?

举个例子。这假设您正在使用通过 OAuth (rather than from a private app) and that you're looking to upload an image from a URL. The access token, shop URL and product ID will need to be swapped for your own. You can find the Product Image API docs here 获得的访问令牌。希望这对您有所帮助!

<?php
$token = 'youraccesstoken';
$ch = curl_init("https://yourstore.myshopify.com/admin/products/934765124/images.json");
$image = json_encode(array('image'=> array('src' => 'https://cdn.shopify.com/shopify-marketing_assets/static/shopify-default.png')));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $image); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"X-Shopify-Access-Token: $token"
));
$result = curl_exec($ch);
GLOBAL $token ='put token';
GLOBAL  $store_name = 'put store name';

$value = 'put your product of array';
$valuemetafield = array(
    array(
        'namespace' => 'Product Parent Style',
        'key' => $value['ParentStyle'],
        'value' => 'value',
        'value_type' => 'string'
    )
);
$value['metafields'] = $valuemetafield;

$ch1 = curl_init("https://".$store_name.".myshopify.com/admin/products.json");
$create_product = json_encode(array ('product' => $value));
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch1, CURLOPT_POSTFIELDS, $create_product); 
curl_setopt($ch1, CURLOPT_HTTPHEADER, array (
"Content-Type: application/json",
"X-Shopify-Access-Token: $token"
));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch1);
$savedProductArray = json_decode($result, TRUE);
$savedProduct = $savedProductArray['product'];