【问题标题】:Can't POST to Shopify Private App using PHP+cURL无法使用 PHP+cURL 发布到 Shopify Private App
【发布时间】:2023-04-09 04:43:01
【问题描述】:

在我的 PHP 代码中似乎找不到错误。我正在尝试通过 Shopify API 添加客户,下面是我的代码。我使用 Postman 尝试了相同的 URL,它起作用了——当我添加相同的 JSON 字符串时,用户会被添加。所以我知道在 Shopify 中使用正确的权限正确启用了 API。

$API_KEY = '****';
$PASS = '****';
$STORE_URL = '****';
$DATE = "2021-07";
$USER_EMAIL = 'test@test.com';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'@'.$STORE_URL.'/admin/api/'.$DATE.'/customers.json';
              // post to: POST /admin/api/2021-07/customers.json
$data = array('customer' =>
                    array(
                        'first_name' => 'Promo',
                        'last_name' => 'User',
                        'email' => $USER_EMAIL,
                        'accepts_marketing' => 'true',
                    ),
                );
    echo "test post data:";
    echo json_encode($data);
    $session = curl_init( $baseUrl );
    curl_setopt($session, CURLOPT_HEADER, false);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);

    $response = curl_exec($session);
    curl_close($session);

    $json = json_decode( $response, true );
    echo "test return data:";
    var_dump($json);

代码打印以下内容,返回 NULL 并且没有添加客户:

test post data:{"customer":{"first_name":"Promo","last_name":"User","email":"test@test.com","accepts_marketing":"true"}}test return data:NULL

谁能发现我在这里做错了什么?

【问题讨论】:

  • 确保 CURL 正在您的 PHP 服务器上运行。
  • 是的,我也想过!我用过 phpinfo();检查 cURL,它已安装。

标签: php shopify php-curl shopify-api


【解决方案1】:

没关系,我只是想通了。 线 curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT'); 应该 curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多