I'm trying to create an array of amazon product variants using DOM php, My desired array should look like;
["Variant Name":"ASIN number"]
Here is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.amazon.co.uk/dp/B08LZHMQXS?psc=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$html = curl_exec($ch)
curl_close($ch);
$dom = new DomDocument();
$dom->loadHTML($html);
$dom_xpath = new DOMXpath($dom);
$variants = $dom_xpath->query('//*[@class="swatchAvailable" or @class="swatchSelect"]');
foreach($variants as $data){
$input = $data->getAttribute("data-defaultasin");
$inputn = $data->getAttribute("title");
if (!empty($input)) {
preg_match_all('/(.{10})/', $input, $output);
$output1 = str_replace("Click to select ","|",$inputn);
$split = explode("|", $output1);
$json1->SizeVariant3[] = $split[1];
$json1->SizeVariant4[] = $output[0][0];
$json->VariantB = array_combine($json1->SizeVariant3,$json1->SizeVariant4);
}
}
Though my code is working but there are some mistakes in code and it might wont work for all amazon products. so I need suggestions and improvement. and also my output is a json object :
{
"2031 Deep Blue": "B08LZH84TN",
"2031 Khaki": "B08LZHMQXS",
}
while I want it to be an array as I mentioned above.
question from:https://stackoverflow.com/questions/65887903/create-array-from-dom-node-values-in-php