Universal replacement for file_get_contents? (PHP) - php

I'm having a bit of a problem with reading an XML file (RSS feed). I'm using file_get_contents to read the data, which works fine locally on Xampp, but won't work on my server.
I don't want to have to edit my php.ini file, I'm after a out-of-the-box solution if there is one (I want the code to be as portable as possible).
I am currently doing...
// Load Correct Feed
$feed_url = $this->selectFeed($instance);
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
I've had a look for solutions (ie. cURL) but can't find a way that will be supported universally.
Please could someone point me in the right direction (if it is even possible)?

what about...
$xml = simplexml_load_file($file);

Related

Decompress GZIP data from PHP

I've been trying to decompress GZIP data from web server
Currently Using:
EasyPHP
Code Igniter
PHP
My Problem
I haven't decompressed the data , it is actually a String compression not a file and i've been searching throughout the net and it only gives me file examples.
Some of my outputs: I haven't seen errors but i got this output:
org.apache.http.entity.StringEntity#3e583da8" is this an error on my side? I've been searching this thing and found out that is from java
Here is my PHP method:
$gzdata = gzopen($chunkData,"r");
$uncompressed_file = fopen($chunkData,"w");
while($line = gzgets($gzdata,4096))
{
fwrite($uncompressed_file,$line);
}
log_message('debug','decompressed data '.$var);
fclose($uncompressed_file);
gzclose($gzdata);
return $uncompressed_file;
i was trying to point out is that my method above was the method i used in overcoming in decompressing gzip data from the web and it turns out that it doesn't work do i need to configure something on my php or apache something? or do i need to install gz libraries to make it work? Need your professional advice and suggestions . Thank You
Another code Here: that i'm currently using:
$chunkDecoded = base64_decode($data);
//$decodeToJson = http_chunked_decode($chunkDecoded);
//$zipCode =gzinflate(substr($chunkDecoded,10,-8));
$zipCode = gzdecode($chunkDecoded);
$decoded = json_decode($zipCode);
log_message('debug','gzuncompress '.$decoded);

PDF Parser PHP Library Not Working

I'm using the PDF Parser PHP library to parse the text from several PDFs. It works perfectly for a majority of these, but seems to just timeout and stop working for certain PDFs.
This is the code I'm using (straight from their demo page):
<?php
include 'vendor/autoload.php';
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('document.php');
$text = $pdf->getText();
echo $text;
?>
When I replace 'document.pdf' with the URL to this file, it works perfectly as expected.
However, when I replace 'document.pdf' with the URL to this file, it just times out with a blank page.
Any ideas why it would work for one file and not the other?
Thanks in advance for any advice!
yes this "ghost" error I saw it too, nothing even in error_log, nor tripped in try catch very hard to diagnose if you increase the memory_limit in php.ini it goes away, it's either something to do with the bad garbage collection on the developers part or ballooning - i think the latter because my loop failed after 4 pdf's but when I quadrupled available ram it didn't fail after 60

parsing a swf/fla (using php?)

I've written a framework (it's been a year) which will render AS3 code as HTML5
I want to reach into a swf and parse the guts into my framework so that you can upload an AS3 swf and get the HTML5 equivalent back.
Any ideas on how to parse a swf/fla using php?
** edit **
As a reference, google does something similiar to this. However, swiffy only parses AS2 code.
** edit 2 **
For further clarification, I only want to be able to parse a swf for layout props and actionscript. I've got the rest figured out.
Take a look at SWFTools - that can dump the code out of SWFs apparently, and has source code, though you could use the command line utilities from PHP and parse the output to get the actionscript sections out. God knows what you will do with it then.
https://github.com/iborodikhin/php-swiffy/
use this lib and implement like this
require_once('php-swiffy-master/vendor/autoload.php');
$swiffy = new Swiffy\Client();
$html = $swiffy->convert("test.swf");
$myfile = fopen("test.html", "w");
if(!empty($html)){
fwrite($myfile, $html);
fclose($myfile);
}

saving and reading a xml file getting from the other url

may be i am going to ask some stupid question but i don't have any idea about php
that's why i want to know it i never worked in php and now i have to do it so please provide me some useful tips,
i have XML file that is coming from a different URL and i want to save it on the server then i have to read it and extract it to a page in proper format and some modification in data.
You can use DOM
$dom = new DOMDocument();
$dom->load('http://www.example.com');
This would load the XML from the remote URL. You can then process it as needed. See my previous answers on various topics using DOM. To save the file to your server after your processed it, you use
$dom->save('filename.xml');
Loading the file with $dom->load() will only work if you have allow_url_fopen enabled in your php.ini. If not, you have to use cURL to download the remote file first.
Maybe this should be helpfull to you: http://www.php.net/manual/en/function.simplexml-load-file.php
If you're have dificulte to get the XML file from the remote host you can use combine with above simplexml-load-string
$path_to_xml = 'http://some.com/file.xml';
$xml = simplexml_load_string( file_get_content($path_to_xml) );

Parsing XML file through PHP while using XSLT as the main template file

I have a lots (500ish) xml files from An old ASP and VBscript that was running on an old windows server. The user could click a link to download the requested xml file, or click a link to view how the xml file will look, once its imported into their system...
If clicked to view the output, this opened a popup window were the xml filename is passed via URL & using the xslt template file this would display the output.
example url = /transform.php?action=transform&xmlProtocol=AC_Audiology.xml
Now were using PHP5 im trying to get something that resembles the same output.
we started looking into xslt_create(); but this is an old function from php4
I'm looking for the best method to deploy this.
The main php page should check & capture the $_GET['xmlProtocol'] value.
pass this to the xslt template page as data;
were it will be output in html.
a general point in the right direction would be great!
You can find the documentation (+examples) of the "new" XSL(T) extension at http://docs.php.net/xsl.
php
// Transform.php
if(isset($_GET['action']) && $_GET['action'] == 'transform') {
// obviously you would never trust the input and would validate first
$xml_file = AFunctionValidateAndGetPathToFile($_GET['xmlProtocol']);
// Load up the XML File
$xmlDoc = new DOMDocument;
$xmlDoc->load($xml_file);
// Load up the XSL file
$xslDoc = new DomDocument;
$xslDoc->load("xsl_template_file.xsl");
$xsl = new XSLTProcessor;
$xsl->importStyleSheet($xslDoc);
// apply the transformation
echo $xsl->transformToXml($xmlDoc);
}
I had a similar problem about two years ago. I was using PHP5 but needed to use xslt_create(); or an equivalent. Ultimately, I switched to PHP4.
You can probably set your server to use PHP5 everywhere except for files in a certain folder. I believe that's what I did so I could process XSL files using PHP4 but the majority of the site still used PHP5.
It's possible that things have changed in the last two years and PHP5 has better support for something like xslt_create(); ---- I haven't been following recent changes.
Hope this helps!

Categories