Upload XML feed to mysql using PHP - php

I have an XML feed coming in:
<?xml version="1.0" encoding="UTF-8"?><product>
<name>John</name>
<contact_email>john#johnson.com</contact_email>
<contact_telephone>01234 567</contact_telephone>
<url>www.johnsone.com.com</url></product>
I need to get this loaded to MySQL using php - have seen a few examples but all of them take a file saved locally.
My feed is taken from the internet so changes all of the time. Does anybody have any suggestions where to start?
Thanks

First you'll need to define a data model. Then you'll need an xml parser to parse the feed, extract the data and populate your data model. Then you'll need to pass your model object to a DAO which writes the data to your database.

If it's taken from the internet you can just do
<?php
$feedData = file_get_contents('http://mywebsite/myfeed.xml');
?>

if you want to parse the data and store then
create a table for product
parse the xml and get the fields
insert into db or update existing data
so, what's your problem?

To be a bit more specific:
You'll obviously need to set up a table and database structure. I'm going to assume you have, or at least can figure out how to, set this up, and how to write to a database. If not, there are plenty of tutorials on that that should be plenty helpful. You'll need to use PHP's built-in MySQL library.
For parsing the XML you will probably want to use SimpleXML. It's not clear how your feed is coming in, but SimpleXML has the simplexml_load_string function that will let you pass it a string containing an XML document, however you get it, for parsing.
From there, you can just take the parsed XML and write it to your database. Any examples that use SimpleXML with a local file should be pretty easy to adapt using simplexml_load_string instead of simplexml_load_file, and doing whatever you're already (presumably) doing to get the data from this feed.

Related

get web page content using php script and save it in my db

I want to get the content of this 1st and 2nd webpage https://www.goodreads.com/search?utf8=%E2%9C%93&q=%D8%B3%D9%8A%D8%A7%D8%B3%D8%A9&search_type=books
and then store it in my database and then make the list is searchable.so after I googled I found that I can do it like this
$url = "https://www.goodreads.com/search?utf8=%E2%9C%93&q=%D8%B3%D9%8A%D8%A7%D8%B3%D8%A9&search_type=books";
function get_links($url){
$input = file_get_contents($url);
echo $input;
}
get_links($url);
my problem is how can I get the 2nd page content also and how can I store these books in my database to the list searchable
The answer is not that easy...
Options
Getting the pages (Not recommended)
To get a later page you can send the "page argument" in your request:
e.g.:
https://www.goodreads.com/search?page=2&q=%D8%B3%D9%8A%D8%A7%D8%B3%D8%A9&search_type=books&tab=books&utf8=%E2%9C%93
But to get the Elements into a nice structure you need to parse the HTML you get which is realy hard.
Use the API (recommended)
At https://www.goodreads.com/api/index you can find the documentation for goodreads API which returns example as response and is easily parseable.
Parse XML in PHP
If you use the API you can parse the XML-response with SimpleXML.
See example 1 & 2: http://php.net/manual/en/simplexml.examples-basic.php
Saving to database
If you are a beginner you might read some tutorials about how to use mysql with php and PDO. But you may also have a look at RedBeanPHP which is an really easy to use ORM for databases.
For getting 2nd page you will have to append on the url ?page=2
In case that you need to get other pages, you can use a for loop if you don't know how to use for loop, please google php for loop
For the database, google php mysql insert

What is the best way to find a specified element using PHP's SimpleXMLElement class?

The project that I'm currently working on involves translating a website that was written in Python to PHP code. The part I'm doing now requires me to send $_GET requests to a web server. The response that I get back is always in XML format, so I'm using the *simplexml_load_file()* function to fetch the result.
I need to parse through the XML that the web server sends back to me. Python had a nice library called BeautifulStoneSoup, which had a find() method that returned the contents of a specified attribute.
After looking at the list of PHP functions at http://php.net/manual/en/book.simplexml.php, I cannot seem to find anything that allows me to specify an attribute name and get the contents. Keep in mind that attribute I may be looking for is not necessarily going to always be one level deep into the XML array. It may be several dimensions into the array.
I can provide an example if this wasn't specific enough. Thanks for the help, friends!
Xpath sounds like what your looking for. It allows for searches to be made within any level of an XML directory.
The following example looks multiple levels deep with an xml directory and checks if the description node contains whatever is held within $txtSearch
$holidayDoc = simplexml_load_file($url);
$qry = "//channel/item[description[contains(text(),\"$txtSearch\")]]";
$holidayDoc->xpath($qry);
Results can then be iterated through using a foreach loop
You can run an XPath query:
$xml->xpath('//#myattribute')

How can I search using PHP through an XML file and display the results?

I am trying to build a very simple price comparison script.
Until now, I wrote a code that gets some product xml feeds from shops and with the help of XSLT I create a single-global xml of all those input XMLs. I use the XSLT because the shops have different names for elements.
Now I want to take it one step further and I want to create a search form that will display me the products let's say I have the term "laptop".
I know how to create a form, but I need a coding guidance to understand how to make it to search in my XML file (products.xml) and display let's say the
Thank you
You might want to check out http://php.net/manual/en/class.xmlreader.php
Using that it is pretty easy to navigate through an XML file and grab all the info you need.
EDIT:
On second thought, http://php.net/manual/en/book.simplexml.php is a MUCH simpler way to achieve what you're trying to do. Hence the name, I guess ;)
You can use SimpleXML library to parse your xml file. In my opinion SimpleXML is easier to use than xmlreader. Though SimpleXML is introduced on php5.

Reading XML into PHP

I'm trying to determine the best course of action for the display of data for a project I'm working on. My client is currently using a proprietary CMS geared towards managing real estate. It easily lets you add properties, square footage, price, location, etc. The company that runs this CMS provides the data in a pretty straightforward XML file that they say offers access to all of the data my client enters.
I've read up on PHP5's SimpleXML feature and I grasp the basic concepts well enough, but my question is: can I access the XML data in a similar fashion as if I were querying a MySQL database?
For instance, assuming each entry has a unique ID, will I be able to set up a view and display just that record using a URL variable like: http://example.com/apartment.php?id=14
Can you also display results based on values within strings? I'm thinking a form submit that returns only two bedroom properties in this case.
Sorry in advance if this is a noob question. I'd rather not build a custom CMS for my client if for no other reason than they'd only have to login to one location and update accordingly.
Some short answers on your questions:
a. Yes you can access XML data with queries, but using XPath instead of SQL. XPath is for XML what SQL is for databases, working quite different.
b. Yes you can build a php program that receives an id as parameter and uses this for an XPath search on a given XML file.
c. All data in a XML file is a string, so it is no problem to search for or display strings. Even your example id=14 is to handle as a string.
You might be interested in this further information:
http://www.ibm.com/developerworks/library/x-simplexml.html?S_TACT=105AGX06&S_CMP=LP
http://www.ibm.com/developerworks/library/x-xmlphp1.html?S_TACT=105AGX06&S_CMP=LP
PHP can access XML not only via SimpleXML but also with DOM. SimpleXML accesses the elements like PHP-arrays, DOM provides a w3c-DOM-compatible api.
See php.net for other ways to access XML, but they seem not to be appropriate for you.

read an xml file into database

I want to store the contents of an xml file in the database. Is there an easy way to do it?
Can i write some script that can do the task for me?
The schema of the XML file looks like this:
<schedule start="20100727120000 +0530" stop="20100727160000 +0530" ch_id="0210.CHNAME.in">
<title>Title_info</title>
<date>20100727</date>
<category>cat_02</category>
</schedule>
One thing to note is:
How do I read the start time? I need the time +0530 added to the time?
Thank you so much.
You'll probably want to create a table called schedules that matches your data, then read the contents of the XML file with an XML parser of your choice. SimpleXML might be the right tool for this job.
As for the dates, I recommend you try using the function date_parse_from_format().
look up simple_xml on the php page - off hand I'm not too hot on it, but basically you will end up with a loop which will add your data to an object eg:
$xml
and you will be able to call tags as such $xml->schedule->title $xml->schedule->date and $xml->schedule->category and you will be able to call attributes as such $xml->schedule[start] but you might wanna check that.
I had to do this recently for a client, and this was the best way I could find. The attributes may be tricky - I can't quite remember but you might have to look into namespaces and such... anyway, find simple_xml and you're on the right tracks.

Categories