Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Problem with array "variant_ids"

Problem with array "variant_ids"

Arenas1
Tourist
9 0 1

Hello, forgive my English...

I am having trouble inserting a variant into a product. I am not very expert and I would not be surprised if it was a syntax problem

 

This is my array in php

$image_array = array(
"image" => array(
"product_id" => $id_producto,
"variant_ids" => array(40582197248208),
"src" => $imagenprincipal,
"width" => 3200,
"height" => 2400,
)


);

 

IT SEND
Array ( [image] => Array ( [product_id] => 6869186216144 [variant_ids] => Array ( [0] => 808950810 ) [src] => xxxx.jpg  [width] => 3200 [height] => 2400 ) ) method=POST

 

call tu fuction TO FUNCTION

$inserta_imagen_principal = shopify_call($token, $shop, "/admin/api/2021-10/products/".$id_producto."/images.json", $image_array, 'POST');

This is the error


{"errors":{"variant_ids":"expected Hash to be a Array"}}

 

I get from the api I have tried every possible way and I can't find it, I appreciate any help I call my array with a curl function that works with many other calls, I still attach it, but I think the problem is that my array has some error.


the function shopify_call

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

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


print 'method='.$method.'<br>';
print $url.'<br>';

// 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);

/*ALELUYA, GRACIAS A ESTE CAMBIO PUEDO ENVIAR TILDES, PERO DESGRACIADAMENTE NO DEVUELVE BIEN EL DECODE*/
curl_setopt ($curl, CURLOPT_POSTFIELDS, utf8_encode($query));
}

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

 

$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]);

}

}



Reply 1 (1)

Arenas1
Tourist
9 0 1

Same error:

 

$params['image'] = [
'product_id'=>$id_producto,
'src'=>$imagenprincipal,
'width'=>3200,
'height'=>2400,
'variant_ids'=> [6869186216144]
];