Parse JSON from webpage - php

I made a php script which connects to my database and parse in JSON format some news. My problem is that the content of the news are in HTML format - with <br> , <img> and so on(so when I open the script - ex: http://localhost/android_connect/get_news.php the browser interprets the HTML tags as it was designed to) .
Is there anyway to parse from JSON the
view-source:http://localhost/android_connect/get_news.php
webpage?

You can read the source of the HTML page with file_get_contents().
$json_news = file_get_contents ("http://localhost/android_connect/get_news.php");
echo $json_news;

Related

php xml SimpleXMLElement addChild big data breaking xml

I am trying to insert base64_encoded pdf content in xml with below code.
1) Getting pdf data in variable.
$varFilePath = 'temp_invoices/' . $varFileName;
$arrFileData = file_get_contents($varFilePath);
$varPdfBase64 = base64_encode($arrFileData);
2) Using base64 data in xml tag
$Attachment->addChild('Attachment', $varPdfBase64);
The variable $varPdfBase64 has too big content and its creating problem.
Now when I am trying to download xml file this is showing plain text instead downloading with xml tags.
Note:- All tags are present in source code(ctrl+u).
How can I resolve this issue?

get data from pdf string and display

I'm creating a pdf from formdata using fpdf (http://www.fpdf.org/).
Using Jquery I'm able to send the data to the pdf-creating script, and make the pdf appear in browser.
Now I'm trying to return the pdf-data to the formpage using fpdf's option to get the pdf as a string and echoing it.
$pdf_file_contents = $pdf->Output("","S");
echo $pdf_file_contents
My question now is
How do I use this data to preview the pdf under the form?
Do I need to use an pdf-viewer or can I use an <iframe> and am I doing this the correct way by returning the variable in an echo?
I can think of two options:
Embed a PDF viewer and handle that call in JS. (Wait for the response from PHP and display the outputted file inline)
Trigger a redirect or load an iframe which displays $pdf->Output($filename,"F") from a temp file.
The result of $pdf->Output() is displayed using the browser's built-in viewer. If there is any output above that in the page FPDF will return an error to say FPDF error: Some data has already been output, can't send PDF.
FPDF error: Some data has already been output, can't send PDF
There might be some other option I'm missing but those would be my first suggestions.

file_get_contents not extracting full file contents

I m using the php function file_get_contents to parse a php file. But it seems that as soon as it is reading the php tags the file_get_contents is malfunctioning.
I checked the function with a normal text file, its functioning perfectly. But even if it finds php tags in a text file, the file is being half read. How can i find a way to get the full contents.
Is the file local? Or are you trying to get a remote file? How did you check that the content is not read? Echoing it to a browser might trick you because of the < char in <?php
Use htmlspecialchars or <pre> to view the whole text. Or just look at the source of the page.

Is it possible to display an RTF file inside a web page using PHP?

I have an RTF file that I want to display inside a web page after tags have been replaced with user input.
I would like to be able to display the RTF file without having to convert it to something before displaying it.
Every time I try it now it gives me the popup open/save box even though I am telling it to display it inline with:
header("Content-type: application/msword");
header("Content-Disposition: inline; filename=mark.rtf");
header("Content-length: " . strlen($output));
echo $output;
Most browsers won't reliably display RTF content. It IS possible to parse the RTF into HTML, and display the HTML content on your web page however.
You need some kind of program to parse RTF and convert it to HTML. I'm assuming it has to be free. I do not know of any reliable free RTF parsing or RTF to HTML libraries in PHP.
I recommend you use a command-line conversion program like RTF2HTML: http://sageshome.net/?w=downloads/soft/RTF2HTML.html
You would need to download and install this program on your webserver, allow the user to upload the file to a temp directory, and then call the command line application from PHP with shell_exec():
$html_output_path = '/path/for/processing/files/'
$html_output_filename = $username . $timestamp;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])
{
shell_exec('rtf2html ' .
escapeshellarg($_FILES['userfile']['tmp_name']) . " " .
$html_output_path . $html_output_filename);
}
$html_to_display = file_get_contents($html_output_path .
$html_output_filename);
Then, parse the results as HTML and display them. Not a bad strategy. Note that you will probably need to remove the head, body and possibly other tags if you're going to display the content inside another web page.
You might want to check out https://github.com/tbluemel/rtf.js for client-side RTF rendering. It's still in its early stages but it renders even embedded graphics. Support for rendering embedded WMF artwork is still very very limited, though, and requires browser support for the tag.
You needed an RTF to HTML converter written in PHP. I think this page contains your solution:
http://www.websofia.com/2014/05/a-working-rtf-to-html-converter-in-php/
First: you've got your content-type wrong. for RTF it's text/rtf
Second: you'll only be able to display in-line this type of content, which can be rendered by the web browser. RTF is not one of these. So you won't be able to display it in-line without converting it, or without some plug-in for the browser. Of course conversion might be on-the-fly.
Web pages can only contain HTML. You would need a browser plugin like flash to display other file types. See Scribd for example.
This isn't exactly an answer to your question, but one thing you may want to do is remove the "filename=mark.rtf" from the header. I've had browsers treat something as a download if I include "filename" in the header, even if the "Content-Disposition" is "inline".
You can't just output a file from within PHP code. You need to extract the data from it, then print the contents inline.
The php function 'file_get_contents' may do what you need. The functions manual is here: http://us2.php.net/filegetcontents
A sample usage is here:
$contents = file_get_contents('yourfile.rtf');

convert html characters back to text in Flash - AS3

I need to generate an editable xml file to supply content to a flash website.
I am generating my file with a html form, and htmlspecialchars e.g.:
$currentItem = htmlspecialchars(stripslashes($currentItem));
This is to prevent xml entries which would produce the error "XML Parsing Error: not well-formed", such as
<entry title="Words & Things">
---------------------^
It has the side effect of making the flash file display the html codes for the content, rather than the proper characters.
Is there a good way to convert the codes back, once they are read into the Flash file (as3)?
Maybe try:
public function htmlUnescape(str:String):String
{
return new XMLDocument(str).firstChild.nodeValue;
}
(Found at: http://www.razorberry.com/blog/archives/2007/11/02/converting-html-entities-in-as3/)
Use html_entity_decode: http://us.php.net/manual/en/function.html-entity-decode.php

Categories