The output of the code inside index.blade.php is this:
Array
(
[0] => <strong>Wheat</strong> Flour
[1] => Tomato Purée
[2] => Mozzarella Cheese (<strong>Milk</strong>) (16%), Pepperoni (10%), Water, Mini Pepperoni (3.5%), Yeast, Dextrose, Rapeseed Oil, Salt, Sugar, Dried Garlic, Dried Herbs, Spice. Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Antioxidants (Extracts of Rosemary, Sodium Ascorbate), Preservative (Sodium Nitrite). Mini Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Sugar, Antioxidants (Sodium Erythorbate, Extracts of Rosemary), Preservative (Sodium Nitrite).
)
Unlike [0] and [1], [2] has many ingredients. I need to be able to separate them all out on to their own rows (using commas and spaces etc as break points). I achieved this in JavaScript doing this: .split(/[:;,.<> /)(]+/);
The result shown here would be a good example: split array values when there is a comma however I get an array to string conversion error when attempting to emulate this.
index.blade.php
require_once 'HTTP/Request2.php';
$request = new Http_Request2('https://dev.tescolabs.com/product/');
$url = $request->getUrl();
$headers = array(
// Request headers
'Ocp-Apim-Subscription-Key' => 'key',
);
$request->setHeader($headers);
$parameters = array(
// Request parameters
'gtin' => '05054402006097',
);
$url->setQueryVariables($parameters);
$request->setMethod(HTTP_Request2::METHOD_GET);
// Request body
$request->setBody("{body}");
try
{
$response = $request->send();
$result = $response->getBody();
//true decodes the json into an associative array instead of stdObject
$decoded = json_decode($result,true);
$ingredients = $decoded['products'][0]['ingredients'];
print_r($ingredients);
}
catch (HttpException $ex)
{
echo $ex;
}