I am creating the following object from the bigCommerce
api as follows:
[
{
"id": 412,
"option_id": 37,
"display_name": "testSteveMemory",
"sort_order": 0,
"is_required": true
},
{
"id": 413,
"option_id": 34,
"display_name": "Hard Drive (desktop)",
"sort_order": 1,
"is_required": true
},
{
"id": 414,
"option_id": 24,
"display_name": "Include Keyboard & Mouse",
"sort_order": 2,
"is_required": true
},
{
"id": 415,
"option_id": 33,
"display_name": "Memory",
"sort_order": 3,
"is_required": true
}
]
I convert this to a PHP array using :
$curlProductOptions = json_decode($curlProductOptions, true);
I then loop through the array and get the option for that option_id
$allOptions = array();
foreach($curlProductOptions as $value){
//echo $value['option_id'].'<br>';
$option_id = $value['option_id'];
$product_url = $url.'/api/v2/options/'.$option_id.'/values.json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $product_url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlProductData = curl_exec($curl);
array_push($allOptions,$curlProductData);
curl_close($curl);
}
I am then using echo to output the array $allOptions and I consume this in a mobile applications as json. if I use
echo $curlProductData.'<br>'
I get the following:
[{"id":112,"option_id":37,"label":"1 gig ram","sort_order":0,"value":"1 gig ram"},{"id":113,"option_id":37,"label":"2 gig ram","sort_order":1,"value":"2 gig ram"},{"id":114,"option_id":37,"label":"4 gig ram","sort_order":2,"value":"4 gig ram"}]
[{"id":104,"option_id":34,"label":"80GB SATA","sort_order":0,"value":"80GB SATA"}]
[{"id":90,"option_id":24,"label":"Yes","sort_order":0,"value":"Yes"},{"id":91,"option_id":24,"label":"No","sort_order":1,"value":"No"}]
[{"id":102,"option_id":33,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":103,"option_id":33,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]
If I use echo any of the following:
echo $allOptions;
json_encode($allOptions);
var_dump($allOptions);
I get a parse error when the data is returned to the mobile app. ie it is not recognising the result as a json array.
I am presuming:
That
$allOptions = array()
declares$allOptions
as an arrayI am populating
$allOptions
with each$curlProductData
in the loop.
What am I doing wrong here ?
I am now using :
echo "[".implode(",
",$allOptions)."]";
I am now getting the following returned :
[[{"id":112,"option_id":37,"label":"1 gig ram","sort_order":0,"value":"1 gig ram"},{"id":113,"option_id":37,"label":"2 gig ram","sort_order":1,"value":"2 gig ram"},{"id":114,"option_id":37,"label":"4 gig ram","sort_order":2,"value":"4 gig ram"}], [{"id":104,"option_id":34,"label":"80GB SATA","sort_order":0,"value":"80GB SATA"}], [{"id":90,"option_id":24,"label":"Yes","sort_order":0,"value":"Yes"},{"id":91,"option_id":24,"label":"No","sort_order":1,"value":"No"}], [{"id":102,"option_id":33,"label":"1GB DDR2 RAM (default)","sort_order":0,"value":"1GB DDR2 RAM (default)"},{"id":103,"option_id":33,"label":"2GB DDR2 RAM (+ $15.00)","sort_order":1,"value":"2GB DDR2 RAM (+ $15.00)"}]]
but I cannot get the mapping correct ?
MrWarby