php code:
<?php
$url = 'https://www.rudolphs-christmasshop.com.au/api/v2/products/';
$username ='xyz'; $password ='ca25fe6947564b9479sdfsaffsaffasfasfsaffdasfe5866b4';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD,$username . ':' . $password);
$result = curl_exec($ch); curl_close($ch);
$xml = simplexml_load_string($result);
?>
output
SimpleXMLElement Object (
[product] => Array (
[0] => SimpleXMLElement Object (
[id] => 29
[name] => SimpleXMLElement Object ( )
[type] => physical
[sku] => 22254
[description] => SimpleXMLElement Object ( )
[search_keywords] => SimpleXMLElement Object ( )
[availability_description] => SimpleXMLElement Object ( )
[price] => 22.9500
[inventory_warning_level] => 5
[warranty] => SimpleXMLElement Object ( )
[weight] => 0.2500
[width] => 13.0000
[height] => 11.0000
[depth] => 8.0000
[view_count] => 125
[page_title] => Aussie Koala and Baby Christmas Ornament - Australiana
[meta_keywords] => koala bear decoration, koala christmas ornament, australian decorations, aussie christmas, christmas decoration
[meta_description] => SimpleXMLElement Object ( )
[layout_file] => product.html
[is_price_hidden] => false
[price_hidden_label] => SimpleXMLElement Object ( )
[categories] => SimpleXMLElement Object (
[value] => 30
)
[downloads] => SimpleXMLElement Object (
[link] => /products/29/downloads )
[images] => SimpleXMLElement Object (
[link] => /products/29/images
)
)
)
)
)
How could I get the image url and display image on browser
May be wrong but I guess like this if I look here
echo $xml->product[0]->downloads->images->link;
But if you show us your XML we are more able to help you.
Greets
I can't test it here. But you can access the tree with:
if you have more products:
<?php
foreach($xml->product as $pout ) {
echo $pout->downloads->images->link;
}
?>
if you want only one product something like this:
$xml->product[0]->downloads->images->link;
Related
<?PHP
$ip = $_SERVER['REMOTE_ADDR'];
$searchterm = str_replace(' ', '%20', $_POST["searchterm"]);
if(isset($_POST["submit"])){
// The request also includes the userip parameter which provides the end
// user's IP address. Doing so will help distinguish this legitimate
// server-side traffic from traffic which doesn't come from an end-user.
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q=".$searchterm."&userip=".$ip;
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, bla.myserver.net);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
}
?>
<form method="POST" action="<?PHP $_SERVER["PHP_SELF"]; ?>">
<p><input type="text" name="searchterm"></p>
<p><input type="submit" name="submit"></p>
</form>
<?PHP
if(isset($_POST["submit"])){
print_r($json);
$jsonNew = $json->{'results'};
print_r($jsonNew);
}
?>
The output for the variable $json is
stdClass Object ( [responseData] => stdClass Object ( [results] => Array ( [0] => stdClass Object ( [GsearchResultClass] => GwebSearch [unescapedUrl] => http://www.speedtest.net/ [url] => http://www.speedtest.net/ [visibleUrl] => www.speedtest.net [cacheUrl] => http://www.google.com/search?q=cache:M47_v0xF3m8J:www.speedtest.net [title] => Speedtest.net by Ookla - The Global Broadband Speed Test [titleNoFormatting] => Speedtest.net by Ookla - The Global Broadband Speed Test [content] => Test your Internet connection bandwidth to locations around the world with this interactive broadband speed test from Ookla. ) [1] => stdClass Object ( [GsearchResultClass] => GwebSearch [unescapedUrl] => https://www.test.com/ [url] => https://www.test.com/ [visibleUrl] => www.test.com [cacheUrl] => http://www.google.com/search?q=cache:S92tylTr1V8J:www.test.com [title] => Platform to Create Organizational Testing and Certifications [titleNoFormatting] => Platform to Create Organizational Testing and Certifications [content] => Test.com is a software solution for you to easily create, administer and manage training courses and certification tests, in up to 22 languages. ) [2] => stdClass Object ( [GsearchResultClass] => GwebSearch [unescapedUrl] => https://en.wikipedia.org/wiki/Test [url] => https://en.wikipedia.org/wiki/Test [visibleUrl] => en.wikipedia.org [cacheUrl] => http://www.google.com/search?q=cache:R94CAo00wOYJ:en.wikipedia.org [title] => Test - Wikipedia, the free encyclopedia [titleNoFormatting] => Test - Wikipedia, the free encyclopedia [content] => Test, TEST or Tester may refer to: Test (assessment), an assessment intended to measure the respondents' knowledge or other abilities ... ) [3] => stdClass Object ( [GsearchResultClass] => GwebSearch [unescapedUrl] => https://www.speakeasy.net/speedtest/ [url] => https://www.speakeasy.net/speedtest/ [visibleUrl] => www.speakeasy.net [cacheUrl] => http://www.google.com/search?q=cache:sCEGhiP0qxEJ:www.speakeasy.net [title] => Speakeasy Speed Test - Powered by MegaPath [titleNoFormatting] => Speakeasy Speed Test - Powered by MegaPath [content] => Speakeasy, a MegaPath Brand, offers industry leading business voice, data and IT solutions. We ensure the speed, performance and reliability of all our ... ) ) [cursor] => stdClass Object ( [resultCount] => 258,000,000 [pages] => Array ( [0] => stdClass Object ( [start] => 0 [label] => 1 ) [1] => stdClass Object ( [start] => 4 [label] => 2 ) [2] => stdClass Object ( [start] => 8 [label] => 3 ) [3] => stdClass Object ( [start] => 12 [label] => 4 ) [4] => stdClass Object ( [start] => 16 [label] => 5 ) [5] => stdClass Object ( [start] => 20 [label] => 6 ) [6] => stdClass Object ( [start] => 24 [label] => 7 ) [7] => stdClass Object ( [start] => 28 [label] => 8 ) ) [estimatedResultCount] => 258000000 [currentPageIndex] => 0 [moreResultsUrl] => http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=test [searchResultTime] => 0.32 ) ) [responseDetails] => [responseStatus] => 200 )
I want to access a property of an object but this is not working:
$jsonNew = $json->{'results'};
print_r($jsonNew);
And by the way it doesn't throw an error or warning. It just displays nothing. Any ideas?
I googled it but I can't find any further information about the Google Search API and how to use the result of search query.
You can access the required element as $json->responseData->results[$x]. where $x is the element number.
Or you could also simply loop through the entire thing as
for($x=0;$x<count($json->responseData->results);$x++){
echo "</br>URL: ";
echo $json->responseData->results[$x]->url;
echo "</br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "</br>Title: ";
echo $json->responseData->results[$x]->title;
echo "</br>Content: ";
echo $json->responseData->results[$x]->content;
echo "</br>";
}
How can I loop through this array and print one result in each line. This is returned by google search api.
This works for me
print_r($rez->responseData);
But this doesn't
print_r($rez->responseData->results); nor this print_r($rez->responseData['results']);
Below is the array
stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[GsearchResultClass] => GwebSearch
[unescapedUrl] => http://www.1860-1960.com/shoes.html
[url] => http://www.1860-1960.com/shoes.html
[visibleUrl] => www.1860-1960.com
[cacheUrl] => http://www.google.com/search?q=cache:4bB2OicXg5EJ:www.1860-1960.com
[title] => Beautiful <b>Antique Shoes</b> and Boots, Vintage Fashions
[titleNoFormatting] => Beautiful Antique Shoes and Boots, Vintage Fashions
[content] => <b>Vintage</b> Clothing. <b>Shoes</b> & boots Hats & bonnets. Parasols, purses, fans & more. Textiles & trims. New Items. ebay auctions. Find out how to order About us Click <b>...</b>
)
[1] => stdClass Object
(
[GsearchResultClass] => GwebSearch
[unescapedUrl] => http://www.ebay.com/sch/Pre1920-Edwardian-Older-/74977/i.html?_nkw=antique+shoes
[url] => http://www.ebay.com/sch/Pre1920-Edwardian-Older-/74977/i.html%3F_nkw%3Dantique%2Bshoes
[visibleUrl] => www.ebay.com
[cacheUrl] => http://www.google.com/search?q=cache:gKFzby7zoS0J:www.ebay.com
[title] => <b>antique shoes</b> | eBay
[titleNoFormatting] => antique shoes | eBay
[content] => eBay: <b>antique shoes</b>. <b>...</b> Quick Look. Antique 1905 Silk Satin Bridal Wedding Shoes Pumps Louis XV Heels Beautiful. Buy It Now $31.90 <b>...</b>
)
[2] => stdClass Object
(
[GsearchResultClass] => GwebSearch
[unescapedUrl] => http://www.amazon.com/Winsome-Wood-Shoe-Antique-Walnut/dp/B000NPQKHO
[url] => http://www.amazon.com/Winsome-Wood-Shoe-Antique-Walnut/dp/B000NPQKHO
[visibleUrl] => www.amazon.com
[cacheUrl] => http://www.google.com/search?q=cache:SBPbbjdSIpMJ:www.amazon.com
[title] => Amazon.com: Winsome Wood <b>Shoe</b> Rack, <b>Antique</b> Walnut: Home <b>...</b>
[titleNoFormatting] => Amazon.com: Winsome Wood Shoe Rack, Antique Walnut: Home ...
[content] => <b>Shoe</b> Rack. With Removable Sink tray, this unique product is nice to have it especially in wet weather.Warm Walnut finish <b>...</b>
)
[3] => stdClass Object
(
[GsearchResultClass] => GwebSearch
[unescapedUrl] => http://trouvais.com/category/antique-shoes/
[url] => http://trouvais.com/category/antique-shoes/
[visibleUrl] => trouvais.com
[cacheUrl] => http://www.google.com/search?q=cache:qPcTFS0bBR4J:trouvais.com
[title] => <b>Antique shoes</b> « Trouvais
[titleNoFormatting] => Antique shoes « Trouvais
[content] => 19th century silk <b>shoes</b>, and then stumbled upon. this tattered silk pair inscribed with a wedding date c1773. <b>antique</b> textiles. I'm happy with just a few tangible <b>...</b>
)
)
[cursor] => stdClass Object
(
[resultCount] => 11,400,000
[pages] => Array
(
[0] => stdClass Object
(
[start] => 0
[label] => 1
)
[1] => stdClass Object
(
[start] => 4
[label] => 2
)
[2] => stdClass Object
(
[start] => 8
[label] => 3
)
[3] => stdClass Object
(
[start] => 12
[label] => 4
)
[4] => stdClass Object
(
[start] => 16
[label] => 5
)
[5] => stdClass Object
(
[start] => 20
[label] => 6
)
[6] => stdClass Object
(
[start] => 24
[label] => 7
)
[7] => stdClass Object
(
[start] => 28
[label] => 8
)
)
[estimatedResultCount] => 11400000
[currentPageIndex] => 0
[moreResultsUrl] => http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=antique+shoes
[searchResultTime] => 0.14
)
)
This is the code that generates this.
<?php
/**
* google_search_api()
* Query Google AJAX Search API
*
* #param array $args URL arguments. For most endpoints only "q" (query) is required.
* #param string $referer Referer to use in the HTTP header (must be valid).
* #param string $endpoint API endpoint. Defaults to 'web' (web search).
* #return object or NULL on failure
*/
function google_search_api($args, $referer = 'http://localhost/test/', $endpoint = 'web'){
$url = "http://ajax.googleapis.com/ajax/services/search/".$endpoint;
if ( !array_key_exists('v', $args) )
$args['v'] = '1.0';
$url .= '?'.http_build_query($args, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// note that the referer *must* be set
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
//decode and return the response
return json_decode($body);
}
print_r($rez->responseData);
Something like:
$obj = new obj(); // your object
function printLies($obj)
{
foreach ($obj as $elem)
{
if (is_array($elem) || is_object($elem))
{
printLies($obj);
}
else
{
echo $elem . '<br />';
}
}
}
Or I didn't understand the question? Maybe you meant this?
$obj = new obj(); // your object
foreach ($obj->result as $result)
{
print_r($result);
}
I'm trying to output the data from an XML file datafeed, the support documents are pretty poor and I'm having trouble looping through the XML.
SimpleXMLElement Object (
[#attributes] => Array (
[version] => 6 [type] => WEB [date] => 20120220 )
[account] => SimpleXMLElement Object (
[#attributes] => Array (
[code] => XXXX
)
[sites] => SimpleXMLElement Object (
[site] => SimpleXMLElement Object (
[#attributes] => Array (
[code] => XXXX
)
[name] => Name [address1] => Address 1 [address2] => town [address3] => UK [county] => County [postcode] => Postcode [phone] => 0123456789 [fax] => 0123456789
[vehicles] => SimpleXMLElement Object (
[vehicle] => Array (
[0] => SimpleXMLElement Object (
[#attributes] => Array (
[code] => 5956 [new] => N
[engineSize] => 998
[mileage] => 8000
[type] => MC
[reg] => XXX
[regDate] => 20070414
[created] => 20110928090130
[modified] => 20110928090130
) [manufacturer] => Honda
[description] => CBR 1000 RR-7
[colour] => Black
[body] => Motorcycle
[fuel] => Petrol
[status] => In Stock
)
[1] => SimpleXMLElement Object (
[#attributes] => Array (
[code] => 5958
[new] => Y
[engineSize] => 125
[type] => MC
[created] => 20110930090254
[modified] => 20110930090254
)
[manufacturer] => Honda
[description] => WW125EX2A
[colour] => BLACK(NHA35)
[body] => SCOOTER
[status] => In Stock
[category] => SCOOTER
)
)
)
)
)
)
The data is loaded by:
$request =
"<"."?xml version='1.0' encoding='iso-8859-1'?".">".
"<download account='XXXX' password='XXXX' version='6' request='EXP' format='XML' dealer='XXXX' vehicles='Y' />";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml", "Content-Length: " . strlen($request)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.catalyst-findit.co.uk/download.php');
$response = curl_exec($ch);
$xml = simplexml_load_string($response);
print_r($xml);
I'm mainly interested in the vehicle data, which I have tried to load by:
echo (string) $xml->vehicles->vehicle[0];
Although this doesn't seem to work.
Be grateful if anybody could offer any advice :)
Thanks in advance.
Edit XML file (displaying only two vehicles for easy reading):
<?xml version="1.0" encoding="iso-8859-1"?>
<findit xmlns="http://www.catalyst-findit.co.uk/download" version="6" type="WEB" date="20120220">
<account code="XXX">
<sites>
<site code="BRI">
<name>XXX</name>
<address1>XXX</address1>
<address2>XXX</address2>
<address3>XXX</address3>
<county>XXX</county>
<postcode>XXX</postcode>
<phone>01472 123456</phone>
<fax>01472 123456</fax>
<vehicles>
<vehicle code="XXX" new="N" engineSize="998" mileage="8000" type="MC" reg="XXX" regDate="20070414" created="20110928090130" modified="20110928090130">
<manufacturer>Honda</manufacturer>
<description>CBR 1000 RR-7</description>
<colour>Black</colour>
<body>Motorcycle</body>
<fuel>Petrol</fuel>
<status>In Stock</status>
</vehicle>
<vehicle code="XXX" new="Y" engineSize="125" type="MC" created="20110930090254" modified="20110930090254">
<manufacturer>Honda</manufacturer>
<description>XXX</description>
<colour>BLACK(NHA35)</colour>
<body>SCOOTER</body>
<status>In Stock</status>
<category>SCOOTER</category>
</vehicle>
</vehicles>
</site>
</sites>
</account>
</findit>
Use XPath to search for specific node data.
$text = <<<EOF
... XML data ...
EOF;
$xml = new SimpleXMLElement($text);
# register xpath prefix for default namespace
$namespaces = $xml->getDocNamespaces();
$xml->registerXPathNamespace('__ns', $namespaces['']);
# get first vehicle data
$result = $xml->xpath('//__ns:vehicles/__ns:vehicle[1]');
# get all vehicles in array
$vehicles = $xml->xpath('//__ns:vehicles/__ns:vehicle');
print_r($result); # or
echo($result[0]->colour);
foreach ($vehicles as $v) {
echo ($v->colour . "\n");
}
I'm trying to read a rss feed and store its value in my database . for this I'm using this
$homepage = file_get_contents('http://showmycode.co.in/supermob/feeds/rss/Jaipur');
$movies = new SimpleXMLElement($homepage);
echo '<pre>';
print_r($movies);
and then I found
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2.00
)
[channel] => SimpleXMLElement Object
(
[title] => supermobdeal
[description] => best discount accross canada
[link] => SimpleXMLElement Object
(
)
[lastBuildDate] => Thu, Nov 3rd 2011, 00:32 GMT
[generator] => supermobdeal
[image] => SimpleXMLElement Object
(
[url] => http://showmycode.co.in/supermob/app/webroot/img/site_logo.png
[title] => supermobdeal
[link] => http://showmycode.co.in/supermob/app/webroot/img/site_logo.png
[description] => Feed provided by supermobdeal
)
[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => Let your
[link] => http://showmycode.co.in/supermob/homes/index/Jaipur/25
[small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/
[description] => trainers r any course
[start_date] => 2011-10-26
[end_date] => 2011-11-30
[start_time] => 09:26:00
[end_time] => 09:26:50
[price] => 50$
[value] => 199$
[discount] => 74.87%
[city] => Jaipur
)
[1] => SimpleXMLElement Object
(
[title] => Feel a
[link] => http://showmycode.co.in/supermob/homes/index/Jaipur/31
[small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/3b8d0_Jaip_OrientSpa_24thOct_M_1.jpg
[description] => Orient Spa
[city] => Jaipur
)
[2] => SimpleXMLElement Object
(
[title] => Feel a
[link] => http://showmycode.co.in/supermob/homes/index/Jaipur/26
[small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/0a646_Bombay-Biryani1.jpg
[description] => Taxes
[city] => Jaipur
)
)
)
)
how can I store tittle , link or other attributes in my database and store image in folder
//get contanints from SimpleXMLElement Object
foreach($movies->channel->item as $opt){
$title= $opt->title;
$tittle=mysql_real_escape_string($title); //for Escapes special characters in a string you can use for all varriables if require
$link=$opt->link;
$des=$opt->description;
$image=$opt->medium_image_url;
//for store image from http url to my folder
$ch = curl_init($image);
$fp = fopen('images/'. time () .'.'.'jpg', 'wb');//path for store image
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
include('db.php');//config file for db connection
$sql="INSERT INTO rss (tittle, link, image,description,city)
VALUES ('$tittle', '$link', '$image','$des','$city')";//insert record in database table
$result=mysql_query($sql) or die('Error, insert query failed');
this code store varriables value in your database and image in folder from http url.thanks..
I have been searching google for over an hour and am frustrated, This seems so simple. All I am trying to print is my accountId. Here is the xml i am being returned from the server:
SimpleXMLElement Object
(
[accounts] => SimpleXMLElement Object
(
[account] => SimpleXMLElement Object
(
[billingStreet] => SimpleXMLElement Object
(
)
[billingCity] => SimpleXMLElement Object
(
)
[billingState] => SimpleXMLElement Object
(
)
[billingPostalCode] => SimpleXMLElement Object
(
)
[billingCountry] => SimpleXMLElement Object
(
)
[city] => Los Angeles
[accountId] => XXXXX
[companyName] => SimpleXMLElement Object
(
)
[country] => United States
[email] => XXXXX
[enabled] => 1
[fax] => SimpleXMLElement Object
(
)
[firstName] => XXXXX
[lastName] => XXXXX
[multiClientFolder] => 0
[multiUser] => 0
[phone] => XXXXX
[postalCode] => XXXXX
[state] => CA
[street] => XXXXX
[title] => SimpleXMLElement Object
(
)
[accountType] => 0
[subscriberLimit] => 250000
)
)
[total] => 1
[limit] => 20
[offset] => 0
)
All i want is accountId. I am using this and it doesnt print anything:
$ch=curl_init("https://app.sandbox.icontact.com/icp/a/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$buf = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($buf);
$aid=$xml->$accounts->$account->$accountId;
print($aid);
i can print the entire xml array just fine with print_r though. Im not sure what i am doing wrong
Maybe $aid=$xml->accounts->account->accountId;?
If there is the possibility of getting more than one account in the response, you can add an array index to the account element:
$aid = $xml->accounts->account[0]->accountId;
Or iterate over the accounts:
foreach ($xml->accounts->account as $account) {
...
}
Also note that $aid will be of type SimpleXMLElement. In most cases you can use this as-is and it will be automatically cast to the appropriate type, but if you want the value as a string you can use an explicit cast:
$aid = (string) $xml->accounts->account[0]->accountId;