0
votes

What is wrong with my array? Im trying to add product on shopify using the API. But it does not add the Price and Image of the product.

Here's the example array:

    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
                        )


                )

        )

)

And here'is the code that i call to add it on 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]);

}

}

The array above is the value of $query variable. What do you think is the problem?

1
Don't switch off CURLOPT_SSL_VERIFYPEER. It is a big security risk. - Dharman

1 Answers

0
votes

I am checking the same all thing are fine Please check your token and endpoints

Please update your image url like : https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201809210816

you can try my code it might be help you

    <?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()));
    ?>