How to Nalpac api products import into shopify

Kumar2573
Pathfinder
92 5 9

<?php
function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, true);
if ($data) {
$data_string = json_encode($data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
}
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($curl);
}

$product = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/product?pageNumber=1&pageSize=5&excludeDiscontinued=true"));

 

echo "<pre>";

$product_array = array();
foreach($product->Products as $products){
$single_array = array();

 

$single_array['product']['title'] = $products->Name;
$single_array['product']['body_html'] = $products->Description;
$single_array['product']['product_type'] = "";
$single_array['product']['vendor'] = "";

 

$single_array['product']['tags'] = "";
if(isset($products->Categories) && !empty($products->Categories)){
foreach($products->Categories as $tags)
{
if(isset($tags->Name)){
$array = explode(' ', $tags->Name);
if(isset($array[0]) && !empty($array[0])){
$single_array['product']['tags'] = " cat_".$array[0];
}
}
}
}

 

foreach($products->Attributes as $Attributes){

if($Attributes->Name == "Product Type" ){

$single_array['product']['product_type'] =$Attributes->Value;
}
if($Attributes->Name == "Manufacturer" ){
$single_array['product']['vendor'] = $Attributes->Value;
}
}

 

$single_array['product']['published_scope'] = "global";
$single_array['product']["images"][0]['src'] = "https://az594929.vo.msecnd.net/images/large/".$products->Sku.".jpg";
$single_array['product']["images"][0]['position'] = "1";

 

$single_array['product']["variants"][0]['option1'] = "";
$single_array['product']["variants"][0]['price'] = $products->WholesalePrice;
$single_array['product']["variants"][0]['sku'] = $products->Sku;
$single_array['product']["variants"][0]['weight_unit'] = "g";
$single_array['product']["variants"][0]['weight'] = $products->Weight;
$single_array['product']["variants"][0]['inventory_management'] = 'shopify';
$single_array['product']["variants"][0]['inventory_policy'] = 'continue';
$single_array['product']["variants"][0]['inventory_quantity'] = $products->AvailableQuantity;

$url = "Put here api url";

$ch = curl_init($url);


curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($single_array));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
print_r($result);

}

print_r($product_array); die;



die;

 

 

?>