Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Last active January 4, 2016 09:39
Show Gist options
  • Save marcosfreitas/8603906 to your computer and use it in GitHub Desktop.
Save marcosfreitas/8603906 to your computer and use it in GitHub Desktop.
<?php
header('Content-Type: text/html; charset=ISO-8859-1');
//header('Content-Type: application/json; charset=utf-8');
ini_set('display_errors', 1);
error_reporting(E_ALL);
ob_clean(); //apaga o buffer de saída
/* Definições */
/*=====================================================================
* json_decode(obj_json, true/false) - #2 argum. é para definir
* se o obj_json será transf. para um array (true) ou ficará como
* um objeto PHP (false);
* O padrão é false;
structure of array items
$array['items'] = array(
0 => array('title'=>'produto 1', 'description'=>'um produto qualquer', ... ),
1 => array('title'=>'produto 1', 'description'=>'um produto qualquer', ... ),
...
)
json structure
{
items:[
{'title':'produto 1','description':'um produto qualquer', ... },
{'title':'produto 1','description':'um produto qualquer', ... },
...
]
}
Exemplo em PHP
$a['items'] = array(
0 => array('title'=>'produto 1', 'description'=>'um produto qualquer'),
1 => array('title'=>'produto 1', 'description'=>'um produto qualquer')
);
$b = json_encode($a);
var_dump($b);
$c = json_decode($b, false);
echo $c->{'items'}[0]->{'description'};
echo '<br>';
$d = json_decode($b, true);
echo $d['items'][0]['title'];
======================================================================*/
// integração php (simple xml com api do youtube)
// read feed into SimpleXML object
$sxml = simplexml_load_file('http://generalorthodontics.com.br/index.php?option=com_productxport&view=server&code=google.com&tmpl=component');
if(!$sxml) echo 'Tente novamente mais tarde.';
$item_attributes = array();
$items = array();
$limite_of_items = 100;
$qty_item = $sxml->channel->item->count(); //quantity of nodes 'item'
echo 'Quantity of children "item" nodes: '.$qty_item.'<br>';
// iterate over entries (in tag entry) in feed
$counter = 0;
foreach ($sxml->channel->item as $item) {
// increment counter and break the loop
// limit + 1
$counter++;
if($counter >= $limite_of_items+1){ break;}
$title = $item->title;
$description = $item->description;
$product_link = $item->link;
// get nodes in g: namespace for item information
$item_info = $item->children('g',TRUE);
// get item values in g: namespace
$condition = $item_info->condition;
$price = $item_info->price;
$image_link = $item_info->image_link;
$availability = $item_info->availability;
$google_product_category = $item_info->google_product_category;
// adding attributes of the current item into position on array
$item_attributes[] = array('title'=>$title, 'description'=>$description, 'product_link'=>$product_link, 'condition'=>$condition, 'price'=>$price, 'image_link'=>$image_link, 'availability'=>$availability, 'google_product_category'=>$google_product_category);
$items = $item_attributes;
}//fim foreach
$items['items'] = $items;
shuffle($items['items']);
//array of group items
//embaraçando o array
//$items['items'] = $item_attributes;
echo($items['items'][0]['title']);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment