I am stuck now, I tried to parse JSON API Google Book using PHP.
this is my PHP
<?php
$api = 'AIzaSyB2__dPng5IGT9Nlca0UT7FxN459mrITmo';
$book_api = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=fift+shades+of+grey&key='.$api);
$book = json_decode($book_api);
$a = $book->items;
$results = $a->volumeInfo;
$images = $results->imageLinks;
foreach ($results as $result) {
$title = $result->title;
$author = $result->authors;
$desc = $result->description;
$cat = $result->categories;
$pages = $result->pageCount;
}
foreach ($images as $image) {
$thumb = $image->thumbnail;
}
?>
<html>
<head>
<title>Exampe API Google Book</title>
</head>
<body>
<h1><?php echo $title; ?></h1><br/>
<?php echo $desc; ?>
</body>
</html>
I get error
Notice: Trying to get property of non-object in
and
Warning: Invalid argument supplied for foreach() in
I want to use to display the search results book.
Can you help me to fix it?
Thanks
You were not selecting the right property of array. I have given a complete solution for your reference below. You really don't need two loops for it.
<?php
$api = 'AIzaSyB2__dPng5IGT9Nlca0UT7FxN459mrITmo';
$book_api = file_get_contents('https://www.googleapis.com/books/v1/volumes?q=fift+shades+of+grey&key='.$api);
$book = json_decode($book_api);
$meta_data = $book->items;
?>
<html>
<head>
<title>Exampe API Google Book</title>
</head>
<body>
<?php
for($i = 0; $i < count($meta_data); $i++)
{
$title = $meta_data[$i]->volumeInfo->title;
$author = $meta_data[$i]->volumeInfo->authors;
$desc = $meta_data[$i]->volumeInfo->description;
$cat = #$meta_data[$i]->volumeInfo->categories;
$pages = #$meta_data[$i]->volumeInfo->pageCount;
$thumb = $meta_data[$i]->volumeInfo->imageLinks->thumbnail;
echo "<h1>$title</h1><br/>";
echo "<p><img src=\"$thumb\" style=\"float:left; padding: 10px;\">$desc</p><br clear=\"all\" /><hr />";
}
?>
</body>
</html>
If you look at your resulting json -
...
items": [
{
"kind": "books#volume",
"id": "V1Re
...
This $a = $book->items; returns an array.
So doing this is wrong -
$results = $a->volumeInfo;
You need to iterate through $a using a foreach and get each individual volumeInfo and modify the rest of your code accordingly
Related
I'm trying to get data from an XML file using simple_xml , so far I can get all the data except the images . How can I call a single image name ?
<?php
$ur="http://services2.jupix.co.uk/api/get_properties.php?clientID=35871cc1b6d9ec6237aaaf94aa0e0836&passphrase=cvYG9f";
$xml = simplexml_load_file($ur);
foreach ($xml->property as $property):
var_dump($property->images->image);
echo 'images->image">'; // this is not displaying
endforeach;?>
My code output as the image below . How can i display image number 1
public 1 => string 'http://media2.jupix.co.uk/v3/clients/657/properties/1356/IMG_1356_9_large.jpg' (length=77)
I think SimpleXMLElement::xpath can do what you are looking for:
You can give this a try:
<?php
$ur="http://services2.jupix.co.uk/api/get_properties.php?clientID=35871cc1b6d9ec6237aaaf94aa0e0836&passphrase=cvYG9f";
$xml = simplexml_load_file($ur);
$image = $xml->xpath('//property/images/image[#modified="2014-07-23 14:22:05"]')[1]->__toString();
var_dump($image);
Or you can loop through all the images and check for the name the you are looking for:
$images = $xml->xpath('//property/images/image');
foreach ($images as $image) {
$url = $image->__toString();
if (false !== strpos($url, "_9_large.jpg")) {
var_dump($url);
}
}
If you want to get the second image of each /images section, then you could do it like this:
$images = $xml->xpath('//property/images');
foreach ($images as $image) {
if (isset($image->children()[1])) {
var_dump($image->children()[1]->__toString());
}
}
Thanks Guy I found solution to my problem .
Looking at the back at the question it seems I did not put it in a proper way
All i wanted is to display images within that section. Xpath was not necessary But i have learned from it . Here is my solution if you can improve it you are much welcome.
$url ="http://services2.jupix.co.uk/api/get_properties.php?clientID=35871cc1b6d9ec6237aaaf94aa0e0836&passphrase=cvYG9f";
$xml = simplexml_load_file($url);
foreach ($xml->property as $property):
?>
<li>
<h3> <?php echo $property->addressStreet;?> </h3>
<?php
$imgCount = count($property->images->image);
for ($i=0; $i < $imgCount; $i++) { ?>
<img src="<?php echo $property->images->image[$i];?>">
<?php } ?>
<p><?php echo limit_text($property->fullDescription,30);?></p>
<h4>£ <?php echo $property->price;?> </h4>
</li>
<?php endforeach; ?>
I've been trying to fix this error for the longest time now, and I just can't seem to fix it.
I'm trying to get an article image, url, and url title. For some reason I keep getting the above error for this code:
<?php
$html = file_get_html("http://articlesite.com/");
if($html){
foreach ($html->find('.index_item a img') as $div) {
$articlePoster = $div->src;
$grabURL = $html->find('.index_item a');
/*Error Here -->*/$articleURL = $grabURL->href;
/*And Here -->*/$rawTitle = $grabURL->title;
echo '<div class="articleFrame"><img src="'.$articlePoster.'" width="125" height="186"/><br><p class="title">'.$rawTitle.'</p></div>';
}
}else{
echo '<h1>'."Sorry.".'</h1>';
}
?>
Any ideas? Thanks.
$html->find('xxxxx') returns an array, so you need to iterate through it -- i.e.
foreach ($html->find('.index_item a img') as $div) {
$articlePoster = $div->src;
foreach ($html->find('.index_item a') as $grabURL) {
$articleURL = $grabURL->href;
$rawTitle = $grabURL->title;
(etc.)
I'm trying to filter out some unwanted Instagram images based on a review score that is a part of the photo caption.
I'm using the Instagram PHP API by cosenary for it and I have the data, and I have been able to filter out the entries with the correct syntax for a review score (*/10).
The problem is trying to get the objects back into json form so I can cache it so I dont have to keep making a million requests to Instagram.
I am trying to store the objects in an array and then encode the array back to json, but I keep getting the error in
Trying to get property of non-object in /Users/***/Sites/instagram/index.php on line 16
Here is what I have so far.
<?php
require_once('instagram.class.php');
$app_key = '<removed>';
$instagram = new Instagram($app_key);
$media = $instagram->getTagMedia('kittens');
$pattern = '/\d+\/+10/';
$fulldata = array();
if($media) {
do {
foreach ($media->data as $entry) {
if(is_object($entry) && preg_match($pattern, $entry->caption->text)) {
$fulldata[] = $entry;
}
}
} while ($media = $instagram->pagination($media));
}
?>
<pre>
<? echo json_encode($fulldata); ?>
</pre>
I'm not sure if that is_object($entry) does much.
This should work
<?php
require_once('instagram.php');
$app_key = '';
$instagram = new Instagram($app_key);
$media = $instagram->getTagMedia('cats');
$pattern = '/\d+\/+10/';
$fulldata = array();
if($media) {
do {
foreach ($media->data as $entry) {
$text = $entry->caption->text;
if(preg_match($pattern, $text)) {
$fulldata[] = $entry;
}
}
} while ($media = $instagram->pagination($media));
}
?>
<pre>
<? echo json_encode($fulldata); ?>
</pre>
I'm trying to print my key/value pairs of data in my CodeIgniter view. However, I'm getting the following error. What I'm I doing wrong?
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/search_page2.php
Line Number: 8
application/controller/search.php
// ...
$this->load->library('/twitter/TwitterAPIExchange', $settings);
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?username=johndoe';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$data['url'] = $url;
$data['getfield'] = $getfield;
$data['requestMethod'] = $requestMethod;
$this->load->view('search_page2', $data);
// ...
application/views/search_page2.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Twitter Test</title>
</head>
<body>
<?php print_r($data); ?>
<?php foreach ($data as $key => $value): ?>
<h2><?php echo $key . ' ' . $value . '<br />'; ?></h2>
<?php endforeach ?>
</body>
</html>
to get the data array accessible in the view do like
$data['url'] = $url;
$data['getfield'] = $getfield;
$data['requestMethod'] = $requestMethod;
$data['data'] = $data;
$this->load->view('search_page2', $data);
else only the variables with names as its keys will be available in the view not the data variable we pass.
update:
this is in response to your comment to juan's answer
Actually if you are trying make it working in the other way proposed.
controller code will be having no change from the code you posted.
$data['url'] = $url;
$data['getfield'] = $getfield;
$data['requestMethod'] = $requestMethod;
$this->load->view('search_page2', $data);
but in the view code you will need to just do.
<h2>url <?PHP echo $url; ?><br /></h2>
<h2>getfield <?PHP echo $getfield; ?><br /></h2>
<h2>requestMethod <?PHP echo $requestMethod; ?><br /></h2>
instead of the foreach loop as your keys in $data are already available as respective named variables inside the view.
The variables to use in your template are
$url, $getfield, $requestMethod
$data is the container for the variables that are passed to the view and not accessible directly
If you do need $data accessible to the view, use a different wrapper object
$container = array();
$container ['url'] = $url;
$container ['getfield'] = $getfield;
$container ['requestMethod'] = $requestMethod;
$container ['data'] = $data;
$this->load->view('search_page2', $container);
I'm trying to integrate a tracking pixel into the magento success page. For testing I build the following code and implemented it into line > 45 in success.phtml file within the template folder. Actually the variables are all empty. What's wrong?
<?php
<?php
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$skus = array();
$qtys = array();
$amounts = array();
foreach ($order->getAllItems() as $item){
$skus[$item->getProductId()] = $item->getSku();
$names[$item->getProductId()] = $item->getName();
$qtys[$item->getProductId()] = $item->getQtyOrdered() * 1;
$amounts[$item->getProductId()] = $item->getRowTotal() * 100;//or $item->getPrice()*$item->getQtyOrdered();//but this will ignore any applied coupons
}
$skuspipe = implode("|", $skus);
$namespipe = implode("|", $names);
$qtyspipe = implode("|", $qtys);
$amountspipe = implode("|", $amounts);
<!--
OrderID: <?php echo $orderID; ?>
skus: <?php print_r($skus); ?>
names: <?php print_r($names); ?>
qtys: <?php print_r($qtys); ?>
amounts: <?php print_r($amounts); ?>
skupipe: <?php echo $skupipe; ?>
namespipe: <?php echo $namespipe; ?>
qtyspipe: <?php echo $qtyspipe; ?>
amountspipe: <?php echo $amountspipe; ?>
-->
Thank you!
In Collections, Magento often only loads kind of a stub of data for each item.
You could load the whole objects by using
$item->load( $item->getId() );
on each iteration.
Also, try to debug output the collection first to see if there are any items found.