使用Variant和Image Shopify API PHP添加产品

时间:2019-09-09 11:25:13

标签: php shopify

我的阵列出了什么问题?我正在尝试使用API​​在shopify上添加产品。但这不会添加产品的价格和图片。

这是示例数组:

    Array
(
    [product] => Array
        (
            [title] => TITLE
            [body_html] => <p><strong>DESCRIPTION</strong></p>
            [vendor] => TESTSTORE
            [product_type] => 
            [tags] => 
            [published] => 1
            [variants] => Array
                (
                    [0] => Array
                        (
                            [price] => 1160
                        )

                )

            [images] => Array
                (
                    [0] => Array
                        (
                            [src] => urlofimage.jpg
                        )

                    [1] => Array
                        (
                            [src] => urlofimage.jpg
                        )


                )

        )

)

这是我在shopify上添加它的代码:

function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {

// Build URL
$url = "https://" . $shop . $api_endpoint;
if (!is_null($query) && in_array($method, array('GET',  'DELETE'))) $url = $url . "?" . http_build_query($query);

// Configure cURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
// curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

// Setup headers
$request_headers[] = "";
if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);

if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) {
    if (is_array($query)) $query = http_build_query($query);
    curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
}

// Send request to Shopify and capture any errors
$response = curl_exec($curl);
$error_number = curl_errno($curl);
$error_message = curl_error($curl);

// Close cURL to be nice
curl_close($curl);

// Return an error is cURL has a problem
if ($error_number) {
    return $error_message;
} else {

    // No error, return Shopify's response by parsing out the body and the headers
    $response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);

    // Convert headers into an array
    $headers = array();
    $header_data = explode("\n",$response[0]);
    $headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
    array_shift($header_data); // Remove status, we've already set it above
    foreach($header_data as $part) {
        $h = explode(":", $part);
        $headers[trim($h[0])] = trim($h[1]);
    }

    // Return headers and Shopify's response
    return array('headers' => $headers, 'response' => $response[1]);

}

}

上面的数组是$ query变量的值。您认为出了什么问题?

1 个答案:

答案 0 :(得分:0)

我正在检查所有东西都一样 请检查您的令牌和端点

请更新您的图片网址,例如:https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201809210816

您可以尝试我的代码,这可能会对您有所帮助

    <?php 
    $params = [];
    $params['product'] = [
            'title'=>'TITLEcus',
            'body_html'=>'<p><strong>DESCRIPTION</strong></p>',
            'vendor'=>'TESTSTORE',
            'product_type'=>'',
            'tags' =>'' ,
            'published'=>1,
            'variants'=> [
                [
                    'price'=>1160
                ]
            ],
            'images'=> [
                [
                    'src'=>'urlofimage.jpg',
                ],
                [
                    'src'=>'urlofimage.jpg',
                ]
            ]

        ];

        function shopify_call($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {

            $url = "https://" . $shop . $api_endpoint;


            if (!is_null($query) && in_array($method, array('GET',  'DELETE'))) $url = $url . "?" . http_build_query($query);

            // Configure cURL
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_HEADER, TRUE);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
            // curl_setopt($curl, CURLOPT_SSLVERSION, 3);
            curl_setopt($curl, CURLOPT_USERAGENT, 'ohShopify-php-api-client');
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

            // Setup headers
            $request_headers[] = "";
            $query = in_array($method, array('POST','PUT')) ? json_encode($query) : array();
            $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();

            if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
            curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);

            if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) {
            if (is_array($query)) $query = http_build_query($query);
            curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
            }

            // Send request to Shopify and capture any errors
            $response = curl_exec($curl);
            $error_number = curl_errno($curl);
            $error_message = curl_error($curl);

            // Close cURL to be nice
            curl_close($curl);

            // Return an error is cURL has a problem
            if ($error_number) {
            return $error_message;
            } else {

            // No error, return Shopify's response by parsing out the body and the headers
            $response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);

            // Convert headers into an array
            $headers = array();
            $header_data = explode("\n",$response[0]);
            $headers['status'] = $header_data[0]; // Does not contain a key, have to explicitly set
            array_shift($header_data); // Remove status, we've already set it above
            foreach($header_data as $part) {
                $h = explode(":", $part);
                $headers[trim($h[0])] = trim($h[1]);
            }
            // Return headers and Shopify's response
            return array('headers' => $headers, 'response' => $response[1]);

            }
    }
    var_dump(shopify_call($token, $shop, '/admin/api/2019-07/products.json', $params, 'POST', $request_headers = array()));
    ?>