I would like some help with coding. I am unable to save image in database using the xml tag. Can someone help me in saving the image, which is in 'ContentItem' tag, (parsing from an xml feed from a different website) in the database, please? I want to save the image from URL but save it as image in the database so I can later display it on my website as a image, not URL.
This is XML code:
<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> <item> <title>title</title> <link>link.com</link> <description><![CDATA[<img src=http://www.yourwebsite.com/i.php?k=d88d4e2b336966b538983783230051c7ce6a17ce width=100 height=100> <BR>Article!!!!!<BR>]]></description> <ContentItem Href="http://www.yourwebsite.com/i.php?k=d88d4e2b336966b538983783230051c7ce6a17ce"> <MediaType FormalName="Picture" /> <MimeType FormalName="image/jpg" /> <Property FormalName="caption" value="Nick Carter" /> </ContentItem> And this is my PHP code, so far (it parses and stores everything else, except for image link):
PHP code:
<?php $doc = new DOMDocument(); $doc->load('http://www.yourwebsite.com/index.xml'); $arrFeeds = array(); foreach ($doc->getElementsByTagName('item') as $node) { $itemRSS = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'description' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'ContentItem' => $node->getElementsByTagName('ContentItem')->item(0)->getAttribute('href'), ); array_push($arrFeeds, $itemRSS); } $mysqli = new mysqli('localhost', '', '', ''); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $check=mysqli_query($mysqli,"select * from `arti` where `title`=`title`"); $checkrows=mysqli_num_rows($check); $stmt = $mysqli->prepare("select * from `arti` where title='?'"); if($checkrows > 0){ echo "article already exists."; }else{ if (!$check) { die('Error: ' . mysqli_error($mysqli)); } } if ($stmt = $mysqli->prepare("INSERT INTO `arti` (`title`, `description`, `link`, image) VALUES (?, ?, ?)")) { $stmt->bind_param('ssss', $title, $description, $link, $image);} else {die("Errormessage: ". $mysqli->error);} foreach( $arrFeeds as $RssItem){ $title = $RssItem["title"]; $description = $RssItem["description"]; $link = $RssItem["link"]; $image = $RssItem["ContentItem"]; $stmt->execute(); } printf (" New Record has id %d.\n", $mysqli->insert_id); $stmt->close(); $mysqli->close(); ?> I would really appreciate the help. Thanks.
No comments:
Post a Comment