You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP Curl调用Shopify API添加商品成功但后台无显示排查

问题描述

我编写了一段通过PHP Curl向Shopify添加商品的代码,执行后返回true,但在Shopify后台无法找到对应名称的商品。该接口在Postman中可正常工作,请问这段代码存在什么问题?

代码如下:

if(isset($_POST['add_shopify']))
{
        $title = $_POST['title'];
        $body = $_POST['body_html'];
        $vendor = $_POST['vendor'];
        $type = $_POST['product_type'];
        $price = $_POST['price'];
        $images = $_POST['images'];
        
        $product = array(
            'title'=> $title,
            'body_html' => $body,
            'vendor'=> $vendor,
            'product_type'=> $type,
            "variants"=>[[
                    "price"=> $price
                ]],
            "images" => [
                    [
                        "src"=> $images
                    ]
                ]
        );
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL," https://9e54fc.myshopify.com/admin/api/2022-10/products.json?access_token=*********************" );
        curl_setopt($curl, CURLOPT_FAILONERROR, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);

        curl_setopt($curl, CURLOPT_HTTPHEADER,array(
            'Content-Type' => 'application/json',
        ));
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($product));
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $resp = curl_exec($curl);
        if (curl_errno($curl)) {
        $error_msg = curl_error($curl);
            }
            curl_close($curl);

            if (isset($error_msg)) {
                echo $error_msg;
            }
        $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
        curl_close($curl);
        print_r(json_decode($resp));
问题分析与修复

你的代码存在以下几个关键问题,导致商品无法正确创建:

  • 请求数据格式错误
    Shopify创建商品的API要求请求体是JSON格式,并且需要包裹在product顶级键下,但你的代码犯了两个错误:

    1. 使用http_build_query($product)生成了URL编码的表单数据,而非JSON字符串,这和你设置的Content-Type: application/json不匹配;
    2. 没有将商品数据包裹在product键中,Shopify API无法识别你的请求内容。
      修复方式:将数组外层加上product键,并用json_encode()转换为JSON字符串:
    $postData = json_encode([
        'product' => $product
    ]);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
    
  • 重复关闭Curl句柄
    代码中调用了两次curl_close($curl),第一次关闭后,curl_getinfo($curl)会因为句柄已失效而无法获取正确的HTTP状态码,这会导致你无法排查请求的实际结果。
    修复方式:只在所有Curl操作完成后调用一次curl_close($curl),调整代码顺序:

    $resp = curl_exec($curl);
    $status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); // 先获取状态码
    if (curl_errno($curl)) {
        $error_msg = curl_error($curl);
    }
    curl_close($curl); // 只关闭一次
    
    if (isset($error_msg)) {
        echo $error_msg;
    }
    print_r(json_decode($resp));
    
  • URL开头存在空格
    CURLOPT_URL中的URL开头有一个空格,这会导致请求地址无效,虽然Curl可能自动处理,但仍可能引发异常。
    修复方式:去掉URL开头的空格:

    curl_setopt($curl, CURLOPT_URL,"https://9e54fc.myshopify.com/admin/api/2022-10/products.json?access_token=*********************" );
    
  • CURLOPT_FAILONERROR设置问题
    该选项会让Curl在HTTP状态码≥400时直接返回false,但Shopify返回4xx状态码时通常会包含详细的错误信息,关闭这个选项可以获取到完整的错误响应,方便排查问题:

    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    

内容的提问来源于stack exchange,提问作者Shikha Negi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 07:01:07