如何使用Shopify API取消订单并退款?

时间:2012-11-27 07:23:14

标签: php curl shopify

我一直在尝试使用php中的shopify API取消并退还我的一个订单。 当我向Shopify API发送curl请求时,其响应为空。

这是我的代码:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://3de0c1f89e936d4f52b29aXXXXXXX:8ae242d39b8fde6b0f8fe04fXXXXXXXX@ABC-XYZ.myshopify.com/admin/orders/145785434/cancel.json?amount=45.5");
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_HTTPGET, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$response = curl_exec($ch);
$result = json_decode($response);
curl_close($ch);
echo '<pre>';
print_r($result);
echo '</pre>';
?> 

我错过了什么。请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:0)

这不是代码的问题。您尝试访问的地址(或其间的任何内容)存在问题。您需要自己调试问题。首先,启用标题输出并查看您正在接收的响应。

curl_setopt($ch, CURLOPT_HEADER, true); 

在将数据传递给JSON解码器之前,回收您收到的数据,看它是否已损坏或空白。

echo $response;

答案 1 :(得分:0)

取消 Shopify API 函数应通过 POST 方法发送。 enter image description here

我分享了我用来取消订单的代码:

    $url = "https://API_KEY:API_PASS@YOUR_SHOP.myshopify.com/admin/api/2021-01/orders/$orderId/cancel.json";
            
    $curl = curl_init();
    
    $data = new stdClass();
    $data->reason = 'declined';

    $jsonData = json_encode($data);
    
    //Set the URL that you want to GET by using the CURLOPT_URL option.
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-type: application/json'
    ));
    //curl_setopt($curl, CURLOPT_HEADER, true);
    
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);

    //Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //additional options
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    //Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    //Execute the request.
    $result = curl_exec($curl);

如果您想发送更多参数,请将它们添加到 $data 变量中。示例:

$data->amount = '45.5';
$data->currency = 'USD';

在发送金额时检查货币是否为必填字段。在 Shopify Orders API Docs

上查看更多信息