Shopify API - 使用PHP上传图像

时间:2015-07-18 13:12:40

标签: php image api upload shopify

我正在尝试使用PHP通过API将图像上传到我的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?

2 个答案:

答案 0 :(得分:0)

这是一个例子。这假设您使用通过OAuth(而不是a private app)获得的访问令牌,并且您正在寻找从网址上传图片的权限。访问令牌,商店URL和产品ID需要交换为您自己的。你可以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);

答案 1 :(得分:-1)

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'];