Shopify使用curl php更新主题模板

时间:2016-11-13 17:02:36

标签: php curl shopify

请有人帮我这个,我厌倦了尝试很多方法,但没有得到适当的解决方案。

下面是我的代码我试图使用php和curl更新液体模板文件,但我无法做到。

<?php
    error_reporting(-1);

    $value = 'this is testing';

    //Code for updating file
    //$service_url = 'https://apikeyauthentication@test.myshopify.com/admin/themes/160467719/assets.json?asset[key]=snippets/product-preview.liquid&asset[value]=7788888&theme_id=160467719';

    $service_url = 'https://apikeyauthentication@test.myshopify.com.myshopify.com/admin/themes/160467719/assets.json';

    $curl = curl_init($service_url);

    $curl_post_data = array(
        'asset'=> array(
                "key"   => "snippets/product-akash123.liquid",
                "value" => 'This is testing'
            )
    );

    //echo json_encode($curl_post_data);die;

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/x-liquid'));
    curl_setopt($curl, CURLOPT_PUT, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($curl_post_data));
    //curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
    //curl_setopt($curl, CURLOPT_POSTFIELDS,http_build_query($curl_post_data));

    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);


    $curl_response = curl_exec($curl);
    if ($curl_response === false) {
        $info = curl_getinfo($curl);
        var_dump(curl_error($curl));
        curl_close($curl);
        die('error occured during curl exec. Additioanl info: ' . var_export($info));
    }
    curl_close($curl);
    $decoded = json_decode($curl_response);
    echo "<pre>";print_r(curl_getinfo($curl));
    //echo curl_error($curl);
    echo "<pre>"; print_r($decoded);die('loll');
?>

1 个答案:

答案 0 :(得分:0)

您需要将Content-Type标头设置为application/json。尝试这样的事情:

<?php
$ch = curl_init('https://key:pass@yourstore.myshopify.com/admin/themes/160467719/assets.json');
$asset = array('asset'=> array(
  'key' => 'snippets/yournewsnippet.liquid', 
  'value' => '{% comment %} here is your new snippet {% endcomment %}'
  ));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($asset)); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);