want to upload file through api in shopfy by php code

I want to upload a file snippet-test.liquid onto the shopify theme assest.

this is my code :

    $update_asset = json_encode(
    array(
        "asset" => array(
            "key" => "snippets/snippet-test.liquid",
            "value" => "hello"
        )
    )
);


$api_url = 'https://secret:secret@demo-gandharv.myshopify.com';
$url = $api_url . '/admin/themes/172870546/assests.json';

$session = curl_init();
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($session, CURLOPT_POSTFIELDS,$update_asset);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$response = json_decode($response);

echo "<pre>";
print_r($response); 

Please let me know where i am going wrong. there is no error.but the file is not getting uploaded. anything i am doing wrong?

1 answer

  • answered 2017-10-11 10:05 TBI

    As we check the code, you didn't add "X-Shopify-Access-Token" in the headers. Please apply that in headers and your code must surely work then. Also check your theme id is correct as well as it should be a published theme id.

    Thanks