0

我一直在尝试使用 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>';
?> 

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

4

2 回答 2

0

这不是代码的问题。这是您尝试访问的地址(或介于两者之间的任何地址)的问题。您需要自己调试问题。首先,启用标头输出并查看您收到的响应。

curl_setopt($ch, CURLOPT_HEADER, true); 

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

echo $response;
于 2012-11-27T08:10:54.990 回答
0

取消Shopify API 函数应通过 POST 方法发送。 在此处输入图像描述

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

    $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 订单 API 文档中查看更多信息

于 2021-04-01T14:02:06.883 回答