PHP call a url returning XML/JSON - php

I am absolute beginner in PHP. Sorry for a very basic API question. I am stuck while coding at a point where I need to call a URL which will return me an XML or a JSON. Now I have to capture that in a variable.
For an example, I have written the following code:
class Search {
private $documents = array();
public function __construct() {
$xmlDoc = new DOMDocument();
$xmlDoc->load("solr.xml");
.....
Now I am directly loading an XML. I dont want to do that, instead:
Step1: I want to call a http url which returns me an XML or JSON.
Step2: I need to store that in some variable like xmlDoc above
Step3: and later ofcourse I want to parse it.
I have no issues with step 3 but I just need some pointers or help as to how can I accomplish step 1 and 2?

load should accept a URL as a parameter.
$xmlDoc = new DOMDocument();
$xmlDoc->load('http://example.com/path/to/file.xml');
Or, you can use file_get_contents to download a URL to a string.
$xml = file_get_contents('http://example.com/path/to/file.xml');
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
Or for JSON:
$json = json_decode(file_get_contents('http://example.com/path/to/file.json'));

For doing this with Json you first need a page which will have some json variables.
You can do this by yourself by typing:
$jsonVar = array('var1','var2'); // several variables
echo encode_json($jsonVar);
You can access these variables by typing:
$jsonUrl = 'http://example.com/json.php';
$jsonUrl = json_decode(file_get_contents($jsonUrl));
To display one of these variables you can type:
echo $jsonUrl[1]; // you can use print_r($jsonUrl); //for displaying the right array numbers to access the vars

Related

PHP - DOMXpath does not return any nodes when it should

I am pulling HTML from Selenium, and then extracting data from the HTML using Xpaths.
This is the Xpath:
/html/body/div[2]/div[1]/div/div/div/div/ul/li/div[1]/h3/a
This is my code:
$data = $webdriver->getPageSource();
d($data, $urltemplate);
$doc = new DOMDocument();
$doc->loadHTML($data);
$xp = "/html/body/div[2]/div[1]/div/div/div/div/ul/li/div[1]/h3/a";
$xpatho = new DOMXpath($doc);
$elementsn = $xpatho->query($xp);
d(get_class($elementsn),$elementsn->count(),$xp,$name);
// d() is a custom function like var_dump().
I always get $elementsn->count() = 0.
This is $data:
https://pastebin.com/ahuvkJfN
I am trying to extract those strings like "NAD M10 BLUOS...", "NAD M12 DIRECT DIGITAL..." and so on...
I saved the HTML into a file, and opened it in my browser. I am attaching screenshot of what data I was looking to retrieve (highlighted in blue):
Basically, the HTML page is a product listing, and I am looking to extract all the product names. To confirm, I used Chrome Developer tools, and used the copy full Xpath function. I have the following Xpaths for some of the product names:
/html/body/div[2]/div[1]/div/div/div/div/ul/li[1]/div[1]/h3/a
/html/body/div[2]/div[1]/div/div/div/div/ul/li[3]/div[1]/h3/a
I would guess that this would generalise to:
/html/body/div[2]/div[1]/div/div/div/div/ul/li/div[1]/h3/a
However, I keep on getting a DOMNodeList with count = 0. Why is this so, and how can I check what the error is, if any?
P.S.: This is the original webpage: http://lenbrook.com.sg/3-shop-by-brand#/page-4/price-49-8667
Try changing your $xp
$xp = '//a[#class="product_link"]/text()'

Sending url parameters through file_get_contents returns nothig

I am trying to scrape a website in order to get latitude and longitude for counties in the us(there are 3306 thus why I am trying to do it through code and not manually)
I am using the code below
function GetLatitude($countyName,$stateShortName){
//Create DOM from url
$page = file_get_contents("https://www.mapdevelopers.com/geocode_tool.php?$countyName,$stateShortName");
$doc = new DOMDocument();
$doc->loadHTML($page);
$node = $doc->getElementById("display_lat");
var_dump($doc);
}
GetLatitude("Guilford County","NC");
This returns nothing but if I change the url to get without the parameters like "https://www.mapdevelopers.com/geocode_tool.php" then I can see that $doc now has some information in it but that is not useful because the value I need (latitude) is dependent upon the parameters passed into the url.
How do I solve this issue?
EDIT:
Based on the suggestion to encode the parameters I changed my code to this and now the document contains information but appears as though it is ignoring the parameters
<?
function GetLatitude($countyName,$stateShortName){
$countyName = urlencode($countyName);
$stateShortName = urlencode($stateShortName);
//Create DOM from url
$page = file_get_contents("https://www.mapdevelopers.com/geocode_tool.php?address=$countyName,$stateShortName");
$doc = new DOMDocument();
$doc->loadHTML($page);
$node = $doc->getElementById("display_lat");
var_dump($doc);
}
GetLatitude("Clarke County","AL");
?>
Your issue is that the latitude information etc isn't present on page load, and java script puts it there
You're going to have a hard time trying to run a webpage with JS and scraping it from PHP without something in the middle, maybe re-try this project with something like puppet or phantomjs so you can run your script against a real browser.
Searching the page there is a ajax request to https://www.mapdevelopers.com/data.php
Sending a POST or GET request will give you the response you are looking for

Return external wordpress page as XML Object

I have a CakePHP site for a client, but with the sites blog run on Wordpress (I just redirect to the WP site for the blog). The client now wants a section of the homepage to pull in a snippet from the blog and I am wondering what is the best way to do this. I am currently trying this...
function getPosts($feed_url) {
$content = file_get_contents($feed_url); // get XML string
$feed_object = new xml($content); // load XML string into object
$x = new SimpleXmlElement($content); // load XML string into object
}
getPosts("example.com");
The 'file_get_content' is working great and actually pulling in the html but I cannot get that html into xml. My error message is 'String could not be parsed as XML'. Anyone know the best way to go about this?
You may want to use simplexml_load_string directly.
function getPosts($feed_url) {
$content = file_get_contents($feed_url); // get XML string
$xml = simplexml_load_string($content);
return $xml;
}

Make a Javascript array into JSON

I've tried searching google and nothing is clear, plus you guys are way faster and correct.
I have an array as follows:
var bannertext = new Array();
bannertext[1] = "ladidadi";
bannertext[2] = "blahblahblahblah";
bannertext[3] = "oooaaaaooooaaa";
How do I turn the bannertext array into a JSON encoded array so that I can send it to a PHP page and break it down using the json_decode function, and then how do I access the individual variables?
EDIT: Thanks to Gazler for that first part! My PHP page looks like this:
$bannertext = $_GET['bannertext'];
$banner = json_decode($bannertext);
How do I access each of those strings now? LIke echo $banner[1]; and so on?
You use JSON.stringify() however in IE you will need to include the json2 library as IE has no native JSON parsing.
var bannertext = new Array();
bannertext[1] = "ladidadi";
bannertext[2] = "blahblahblahblah";
bannertext[3] = "oooaaaaooooaaa";
console.log(JSON.stringify(bannertext));
As an aside, you can instantiate an array using the array literal syntax.
var bannertext = [];
Here is a blog post explaining the difference.
JSON.stringify:
JSON.stringify(bannertext);
Older browsers, IE7 and below, require an external library Json2 Free CDN

Zend get db data in xml format using context switching

i am curious to know how can i get the database data in xml format in Zend framework using context switching.
Do i need to compulsorily specify the format in my url, like:
http://localhost/pt/public/index.php/api/v1/users.xml?param1=3
I want to get the format from url (.xml, .json...) and apply the corresponding format to my output automatically.
Currently iam doing this: I get the user data from the database. I get the users marks based on the class id i pass to the url:
$id = $request->getParam('param1'); // get class id param
$users = new Application_Model_DbTable_Users();
$result = $users->fetchData($id);
if(count($result) != 0)
{
$doc = new DOMDocument();
$doc->formatOutput = true;
$root = $doc->createElement("Student");
$doc->appendChild($root);
foreach($result as $details)
{
$root_element = $doc->createElement("Marks");
$root->appendChild($root_element);
$TElement = $doc->createElement("Total");
$TElement->appendChild($doc->createTextNode($details->marks));
$root_element->appendChild($TElement);
}
$xml = $doc->saveXML();
$this->view->xml = $xml;
}
And in the corresponding view script, i have this code:
<?php
header('Content-type: text/xml');
echo $this->xml;
?>
I get the user data and use DOMDocument to write the xml output to the view. But is it possible to automatically generate the XML data from database, without using DOM ?
emaillenin is right, there's nothing ZF can do to convert your data to XML.
But instead of forming XML manually (with DOMDocument and the like), I suggest that you take a look at PEAR XML_Serializer package.
XML_Serializer allows you to transform arrays, objects, etc. to well-formed XML. You can also specify your root name, default tag names, indentation type, etc. So, you're pretty much in control for the resulting XML.
Zend context switching helps in changing your headers, disabling layout and those kind of helps. It does not help in returning the database data as XML data.
The following code can be used to return XML output once you have converted your DB data into XML formatted data with tags.
class OutputController extends Zend_Controller_Action
{
public function xmlAction()
{
$xml = simplexml_load_string($sourceData);
$output = $xml->saveXML();
Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
$this->_helper->layout->disableLayout();
Zend_Layout::getMvcInstance()->disableLayout();
header('Content-Type: text/xml');
echo $output;
exit();
}
}
I will update the answer if you provide more information about how you retrieve the data from DB and what format of XML (the hierarchy of tags) you want in the output.

Categories