I load an XML file from a service provider, and then my HTML displays the images in the necessary place. However, I wish to cache all of these files locally, instead of having the browser load them from the remote server each time.
Here is a sample of my XML file...
feed.xml
<URI>http://imt.boatwizard.com/images/1/14/77/3801477_-1_20120229071449_0_0.jpg</URI>
<URI>http://imt.boatwizard.com/images/1/40/6/3794006_-1_20120814035230_16_0.jpg</URI>
<URI>http://imt.boatwizard.com/images/1/21/74/4012174_-1_20120706051335_21_0.jpg</URI>
Can someone please help me write the PHP to loop through the XML, and download each image.
1) Download image
2) Rename image URL in XML, to match local file.
3) Save XML
Thanks!
I guess you should do something like this
// xmlize your... ehm... xml
$xml = new SimpleXMLElement($xml_content);
// extract uri elements
$result = $xml->xpath('/URI');
// loop through uris
while(list( , $node) = each($result)) {
// with curl functions, download every image
curl_stuff_i_dont_remember($node);
// move it to your folder
rename($downloaded_img, $newpath_img);
// if everything went ok, add a new line into the output xml
$outxml = $outxml . '<URI>' . basename($newpath_img) . '</URI>';
}
// dump the outxml
$fp = fopen('newxml.xml', 'w+');
fwrite($fp, $outxml);
Related
I want to import in the data from the docx file to my CRM, I am using PHP DOMDocument::loadXML method for it, but I am not able to find a way through which I can import the Mathmatical formula and the images from the document file.
The Image of the word file is
The code through which I am trying to import is
<?php
$questionSheetRecord= readDocx("demo-stack.docx");
echo "<pre>";
print_r($questionSheetRecord);
echo "</pre>";
function readDocx($filePath) {
// Create new ZIP archive
$zip = new ZipArchive;
$dataFile = 'word/document.xml';
// Open received archive file
$returnArray=array();
if (true === $zip->open($filePath)) {
// If done, search for the data file in the archive
if (($index = $zip->locateName($dataFile)) !== false) {
// If found, read it to the string
$data = $zip->getFromIndex($index);
// Close archive file
$zip->close();
// Load XML from a string
// Skip errors and warnings
$xml = DOMDocument::loadXML($data, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
// Return data without XML formatting tags
foreach($xml->getElementsByTagName('p') as $child) {
$returnArray[]= $child->nodeValue;
}
$text = $xml->saveXML();
return $returnArray;
}
$zip->close();
}
// In case of failure return empty string
return "";
}
?>
`
Link of word file is
Word File to import data
and link of code is Code Link
The output that I am getting while running this code is
I want these a : along with their post formula and picture.
I have gone through the other questions asked relating this issue but they are not the proper solution for this question.
Thank you
Please go through the "update-v1.1.php" and "demo.docx" file. Here you can get a code for extracting images from the word file, But the drawback in this code is that if you want to add any image in the word file then you have to add a tag "IMG1", here IMG has a post value of the sequence of the image, like if this is the first image in the word file then the tag will be IMG1 and if it is the sixth image then it will be IMG6. When you will run the code you will get the array which you can use in any why you want.
If anyone find any better solution please inform me here only.
Also I am searching a solution of exporting data into word with images at any specified place (means in-between text sentences, I want to insert 1 or more than one images).
I created this script to get the sources of the images from a single webpage, now, i wanted to download all those images to my server using curl.
<?php
// Create DOM from URL or file
require_once 'library/simple_html_dom.php';
$html = file_get_html('http://adamkhoury.com/');
// Find all images
foreach($html->find('img') as $element) //every image found is declared as $element
echo $element->src . '<br>';
?>
It's as simple as this
file_put_contents("filename.extension", file_get_contents($element->src));
There is file_get_contents() function in PHP for getting external contents and file_put_contents() for storing content in a file...
use file_get_contents() to download images and use file_put_contents() to store that:
$image = file_get_contents($element->src);
file_get_contents("image.jpg",$image);
of course, there is another method names cURL in php for downloading external data...
I am new to PHP. I am downloading an XML file from a web service using PHP. I can download the file using this code:
$sourcefile = "http...com?querystring=string";
$destinationfile = 'data\description.xml';
$xml = file_get_contents($sourcefile);
file_put_contents($destinationfile, $xml);
But when I open the XML file, it has < where < should be and > where > should be.
I added this line of code to decode it before saving it to file, which fixes the above problem:
$xml = html_entity_decode($xml);
This doesn't seem to me to be the right way to go about it. Also, I am getting a weird piece of text showing up in the XML file, which prevents me from parsing the XML file:
I tried using str_replace($xml) right before decoding it (and tried it after decoding it), but that wouldn't get rid of it.
What is the correct way to download an XML file using GET from a web service in PHP and will it get rid of that weird string ()?
I have a lot of .doc files with entry specifications for a database. I need to parse through all of these documents and create entries with the information within the documents. I have been trying to use the COM approach. The file has plain text on the top and at the bottom of the page... however, the specifications are in a table at the center of the page. If I don't unlink the new .txt file I can see that the content is transfered to the new document, but it has a bunch of invalid characters in the form of [] running throughout it. When I use file_get_contents() it completely ignores all of the text from the table.
Is there someway to programmatically take care of this? I can't really find any information on the API of the word.application COM object. Ideally I'm thinking I should strip the formatting then save the file as a .txt file or something to that effect.
Any help would be greatly appreciated.
Here is my code:
$dir = $PATH."/scripts/specsheets/doc";
$files = scandir($dir);
foreach( $files as $file ) {
if( strtolower(substr($file, -3)) == "doc" ) {
$word = new COM("word.application") or die("Unable to instantiate Word");
$word->Documents->Open($dir."/".$file);
$new_file = substr($dir."/txt/".$file, 0, -4).".txt";
$word->Documents[1]->SaveAs($new_file, 2);
$word->Documents[1]->Close(false);
$word->Quit();
$word = NULL;
unset($word);
$output = file_get_contents($new_file);
rename($dir."/".$file, $dir."/archive/".$file);
echo utf8_encode($output);
}
}
Can't find a solution using the COM approach... but you can use the antiword program for Windows to get the output if you use this command in php
$content = shell_exec("C:/antiword/antiword.exe ".$filename);
the link for the windows version is:
http://www-stud.rbi.informatik.uni-frankfurt.de/~markus/antiword/
It works very well, it even extracts the data in the tables. Definitely solved my issue.
I want to read content from a text file and download it into a pdf file using PHP object orientation. How I read content from a file, is it same as simple PHP?
The simplest way to get a file’s contents is file_get_contents:
$contents = file_get_contents('filename');
Do you want to know how to get the contents of a file using object-oriented notation, or how to store the contents as an object, or how to move the contents into a pdf?
Assuming the last 2:
//First set the file path and get the contents of the file:
$textfile->path = "path/to/file.txt";
$textfile->contents = file_get_contents($textfile->path);
//Next create the pdf, both as a handler and as a file on disk:
$pdf = PDF_new();
PDF_begin_document($pdf, "file.pdf", "");
//Then put the text file contents into the pdf:
PDF_show($pdf, $textfile->contents);
//Finally, save and close the pdf:
pdf_save($pdf);
pdf_close($pdf);
If you want the script to return the pdf from a request and not save it to the server, simply change "file.pdf" to "" and use the header() function to set the filename.