Can i parse JSON remotely from another server dynamically generated php page? - javascript

I've been attempting to make this work reading everything, but no luck so far. I generate JSON with php for some records using json_encode and I have it output to a .php page on the server - and example is below:
[{"number":1,"name":"Lizzie","image":"http://scienceblogs.com/scientificactivist/wp-content/blogs.dir/392/files/2012/04/i-dcb85296b3695e8ce6d1ae4d660cea30-Smiley-face.gif"}]
On another server, i'd like to ideally read this JSON output and echo it to the page, but I can seem to read it locally but not remotely. Here is an example how far I got with the code:
$.getJSON("http://www.server.com/json.php",function(ajaxresult){
$("#number").append(ajaxresult.Number);
$("#name").append(ajaxresult.Name);
$("#image").append(ajaxresult.Image);
});
How ever this code will read a local .json file and write the elements out - but not a remote one and consequently not the dynamicly generated remote one.
Any hints to what I've done wrong?

Save the dynamically generated JSON as a temporary local file and read it from there.

Related

post not finding php file on local server.

I'm trying to use an post call to a get some data from a php file. I know the code works because I'm using it on another computer and it works fine. Every time I make a call I get this error.
HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).
(XHR)POST - http://localhost/PhpProject4/‪newEmptyPHP.php
This is from MS edge. The php file is in the same directory as the html file and the file making the call. I think it has to do with my xampp server. Any Ideas? Also this is all on my local machine.
Just check your .htaccess file, maybe mod_rewrite changes adress

How to convert html to base64 and save an image on a server with php?

I have a json file like this:
{"user":{"email":"user#test.com"},
"screenshot":{"blobFile":"<!DOCTYPE html><html lang=\"en\">...</html>"}}
and I want to take a screenshot, using XMLHttpRequest sending data a PHP file.
In PHP file getting request like:
$data = json_decode(file_get_contents("php://input"), true);
$htmlStr = json_encode($data["screenshot"]["blobFile"]); // <!DOCTYPE html><html lang=\"en\">...
so far everything is ok but how to convert this string to the image file and save a server?
I've tried html2canvas in PHP file but not fire.
any ideas?
I'd try to use PhantomJS. It's headless browser, which allows to interact with web pages many ways including making screenshots. It will require some time to understand how to work with it, but it definitely will get a result. Although, PhantomJS sometimes is a headache if your page is quite complicated structured, written using some frameworks like ReactJS, AngularJS, etc.
What it does it renders HTML page with styles including scripts serverside. If you save not HTML string but exact URL with COOKIE and SESSION data and then reconstruct conditions which user had in an opened page when you did a screenshot, it'll do a job.
See example here Screen Capture on PhantomJS

Can I get Netbeans 7.3 to debug a PHP script in response to a JSON request?

I'm using Netbeans 7.3.1, and usually I can successfully debug PHP files using Xdebug. However, that's only if my site project is structured in such a way that the site is generated from PHP code right off the bat.
Right now I have a site which is mostly made from HTML files. The HTML static, not generated by PHP. The only PHP file in the project is called phphandler.php. I need to debug that PHP file, but it only runs in response to a Javascript/jQuery call from within the HTML files that looks like this:
$.post(siteURL + 'jsonhandler.php', {
JSON: JSON.stringify(data)
}, processResult, "json");
On the PHP side, it processes the JSON request from $_POST
$Array = json_decode(stripslashes($_POST['JSON']), true);
When I run the debugger from within JSON, it opens Chrome and loads my site, starting with index.html, but the debugger never opens jsonhandler.php. I can go through all the links and navigate through my site, and it will get all the JSON data it needs from jsonhandler.php, but the debugger remains uninvolved.
Can I debug through jsonhandler.php when the Javascript sends it a request via $_POST, and if so, how?
Update: I've discovered that I can debug jsonhandler.php using the Debug File function, but this has serious drawbacks, namely that I can't create a situation in which jsonhandler.php is receiving JSON data as constructed by the rest of the site. I can manually force in some hand made test JSON data, but then that seriously inhibits my ability to discover what the system is doing as a whole.
Anyway, for me this is another indicator that the debugging environment is configured correctly, it's just a matter of if and how the debugger can listen for the right events.
What I normally do is putting break points. Try putting breakpoints to the PHP code that receives the request (or that you want to debug) in the jsonhanler.php file and them run in debug mode, navigate through your site and send the request, the debugger should open Netbeans and take you to the line where you left the breakpoint. From there you can now go line by line or jump into functions.
You can try printing the $Array received in the php file by print_r($Array) followed by an exit() which might stop the process in the php file and show you the result coming in the php file. This might allow you to see as to what the php file is getting when it is loaded in the $Array. Or else if its an ajax request you can print the response in the console by console.log(response). I hope it helps.
I recently responded to a similar question here: https://stackoverflow.com/a/19636910/212076
Basically, you need to add a query parameter within your JSON to trigger the debugger:
XDEBUG_SESSION_START=netbeans-xdebug
If the data is being obtained directly from a HTML Form, then include a hidden field like so:
<input type="hidden" name="XDEBUG_SESSION_START" value="netbeans-xdebug" />

jQuery get url not fixed/static

I am trying to get some xml into a html table. The thing is my xml is created dynamically on the server as a temporary document. How can I call this file if the url can be different? I hope this makes sense. I am new to jQuery, Javascript.
At the time of dynamic generation of the file on the server, also write the path of the file in a static file. Use the client to retrieve the path from the static file and then make the actual request to the dynamically generated file.
hope i understood your question right.

Write back to an XML file

I was wondering if this is possible, I'm not looking for code, i just want to be pointed in the right direction - php, asp or javascript. I have an xml file:
<?xml version="1.0" encoding="utf-8"?>
<Groups>
<Group>
<groupNum></groupNum>
<purgeGroup></purgeGroup>
<DupeRecs>
<DupeRec>
<Name></Name>
<Duplicate></Duplicate>
</DupeRec>
<DupeRec>
<Name></Name>
<Duplicate></Duplicate>
</DupeRec>
</DupeRecs>
</Group>
</Groups>
I would like to be able to load this into a web page and have the Duplicate tag display as a checkbox the user can the check/uncheck whether the record is a duplicate and this is written back to the xml file
Broadly, Here are the steps I would suggest :
"Deserialize" the XML into a corresponding object
Display the editable fields in a HTML form on your Web Page
Process the HTML Form action and update your "Deserialized" object.
"Serialize" the result back into the XML file.
You have to take care of multiple edits on the file using some locking mechanism.
I think the best way would be to while parsing the XML file for display, save it into certain variables that can be easily edited, and set these to the new variables for your XML file. When you have everything how you want it in the variables, re-assemble it and use the fopen(), fwrite() and fclose() commands to re-write the whole file.
EDIT: You could also look into PHP's XMLWriter functions.
I'm not sure what you mean by "load this into a webpage" but to have the Duplicate tag display as a checkbox suggests you want to convert the XML to a UI, for this I'd recommend XSLT which can be used to convert XML to anything including ASPX, PHP or JavaScript.
As for writing this back to the XML again I don't know where the XML file is but it is likely you want to send the user response back to the server somehow and that could be done in many ways.
In conclusion if your XML content is on the server then use XSLT to create a web page to display the UI and send the data back to the server.
If your XML content is client side then try Silverlight.

Categories