I am modifying someones existing coldfusion web app. I am adding php processing pages to do various tasks. Up to this point I have just been calling the php pages, and interacting with the web app by passing variables via the url.
Current usage:
public.cfm calls processing.php?id=69
Then processing will do what it has too, then ultimately:
header("Location: $publichome?id=$id&importantstuff=$stuff");
exit();
And the webapp will pick up where it has too. But now one of my scripts has to send a JSON object back instead of simple variables. I don't know how to get this done. I tried doing a post with cURL but that wasn't working because I need the public facing coldfusion page to take over and curl returns to the php script (I know I can echo the body of the curl result but this keeps me on the php script domain which I dont want). Is there a way to do the above header location and send an object because thats what I need - the php script to stop and the coldfusion page to be served up with the object to work with.
Do I have to create some sort of JSON service in php, that the coldfusion page will call an retrieve the result? I can also modify the coldfusion page any way I want.
You should be able to pass the json string as a URL variable, just like you are passing simpler strings, through the location header. You will need some means of json-serializing the object in php, if you haven't done that yet. There is likely a json library available to do that.
edit
Based on more information, I now have a better suggestion:
From CF, make a <cfhttp> request to your PHP code, passing whatever parameters are necessary to PHP as arguments in cfhttp. From PHP, simply "output" the JSON as the response body. After the CFHTTP call returns to CF, you'll have access to the JSON via the cfhttp.fileContent variable, which you can then run through DeserializeJSON to get back a real object. Here some sample CF code:
<cfhttp url="processing.php?id=69" method="get"></cfhttp>
<cfset importantStuff=DeserializeJSON(cfhttp.fileContent)>
Related
My API written in PHP returns JSON in an api used by a phone.
Depending on certain paramaters, the PHP file either returns the JSON itself or redirects to a different PHP file that returns similar JSON.
In a browser, the JSON returned directly from the starting file looks identical in form to the JSON returned after the redirect. However, the phone is not getting any JSON at all if there is a redirect.
This doesn't make sense to me but it's what I'm seeing. Is it possible for a redirect to mess up a JSON response?
For the record, here is what the JSON looks like in the browser:
{"comment":[{"response":"Hello World"}]}
Edit:
Apparently, redirects in restful APIs are somewhat frowned upon but possible, however, do you have to do something with a status code? This is unfamiliar territory for me.
JSON response redirect
Thanks for any ideas.
You can do redirects using 307 temporary and 308 permanently codes as stated in the IETF draft https://tools.ietf.org/id/draft-hunt-http-rest-redirect-00.html#rfc.section.2.1
Redirection is not the right tool in your case.
As I understand you‘re trying to delegate different endpoints to different files. You should have a look at request routing which happens server side, the client should not have to deal with this.
https://link.medium.com/Sdp3DyXTHX
https://symfony.com/doc/current/components/routing.html
https://www.php-fig.org/psr/psr-7/
I am building an app that extracts data from a website and displays them in my app. I am using PHPQuery to extract data in my server-side code.
However, one page contains an .asp form with two dropdown menus. I need to select an option in both of them and then parse the resulting html. I need to do this server-side, so javascript doesn't seem to be the option.
How can I do so? Can it be done using PHPQuery or some other technology is required?
The page in question is: http://www.bput.ac.in/exam_schedule_ALL.asp
Since you're using PHP and phpQuery, I suggest you also try cURL.
Explore what the form submits via JavaScript and replicate that via cURL. Do this to get the format of the posted (assumption) data, which you can then replicate in a cURL request to the same endpoints. JavaScript won't be necessary, and you can get the same results you need. In this case, you won't need the item mentioned next.
Alternatively, if you have a browser, such as webkit, phantomJS, etc, you can write an automation script to run those steps and return the results, depending on exactly what you need returned. See more complete examples here: https://stackoverflow.com/a/17449757/573688 for how others suggest you do this. NOTE this is not usually necessary if you just need to emulate POST requests.
This is a non-coded answer because it's not entirely clear what direction helps you the most.
On page works JavaScript, which should make an AJAX request to the server and pass the selected value from both SELECT.
The server receives the AJAX request, performs the request to the correct address (you can use phpQuery) and prints the response (gets to the response on AJAX request).
JavaScript on the page receives a response to an AJAX request and performs actions affected.
I have a GAE PHP script that accepts a POSTed message consisting of $_POST['version_name'], $_POST['version_comments'] and $_FILES['userfile']['tmp_name'][0].
It runs a file_get_contents against $_FILES['userfile']['tmp_name'][0] and stores the binary away in a CloudSQL DB.
This is the end point for a PHP-driven form, so users can upload new versions (with names / comments) through a friendly GUI from their browser. It works fine.
Now I want to be able to use the same handler as the end point for a Python script. I've written this:
r = requests.post('http://handler_url_here/',
data={'version_name': "foo", 'version_comments': "bar"},
files={'userfile': open('version_archive.tar.gz', 'rb')})
version_archive.tar.gz is a non-empty file, but file_get_contents($_FILES['userfile']['tmp_name'][0]) is returning null. Uploading files is a bit tricky with GAE, so I'd prefer to not change the listener - is there some way I can make Python send its payload in the same format the listener is expecting?
$_POST['version_name'] and $_POST['version_comments'] are working as expected.
I'd start by looking at the middle-man, which in this case is the HTTP request. Keep in mind, your Python script isn't posting directly to PHP; it's making an HTTP POST request, which is then getting interpreted by PHP into the $_POST variables and whatnot.
Figure out a way to "capture" or "dump" the HTTP request that Python is sending so you can inspect its contents. (You can find a number of free tools that help you do this in various ways. Reading the HTTP request should be pretty self-explanatory if you're familiar with working with $_GET and $_POST variables in PHP.) Then send a supposedly identical request from PHP, capture the HTTP request, and determine how and why they're different.
Good luck!
I am currently working on developing an API for a company. Specifically, here's my issue. They have JavaScript arrays on a webpage that their webmaster updates. I have to pull these arrays into either a simple JS script or PHP file and get the contents of these arrays, which I can then arrange according to the API's specifications and output it as JSON.
How do I pull a JavaScript variable in from a remote page in either PHP or jQuery/JS and make it usable for other applications?
No, I don't have access to the company's website. I have to work off of page scraping for this one.
Thank you!
You can't access private javascript variables remotely, due to Same origin policy. They would have to output the arrays in some kind of readable format that you could access using AJAX, probably as JSON.
Edit: As mentioned below, if the array is explicitly defined as text in a javascript file, you could grab the contents of that file using cURL in PHP
If I where you, I’d use PHP to file_get_contents (or CURL depending on the server config) the page and then parse it based on whatever the markers are to find the value of the variable, assuming it’s written out to the page in the first place.
I need to display information from an SQL database which resides on a server, to a remote webworks mobile device. I am extremely new to passing information from a server so bear with me. My normal understanding is that I would have an HTML file that accesses a php script which then itself connects to the database and displays the information.
However, in webworks the HTML/Javascript files reside on the device and are separated from any php file so I need a method to communicate to get the data from the database. I have looked through JSON and read all the tutorials on w3schools and I understand the syntax but I don't understand how to use it. How could it connect to a database? My aim is to simply display the table entries on a mobile device app running HTML5 webworks. Again I am very new to this so any explanation would be very helpful.
Chances are, you should get a book. This is not something that can be explained in detail in a short answer on this site.
in summary however, you can either
1) send requests to your php script by submitting a form on an html page, which will load a new page filled with whatever PHP sends back. in this case you do not need to use JSON at all as PHP would be returning a full html page.
2) you can use AJAX. AJAX is a javascript method of sending requests to the server (PHP), and getting a response without ever loading a new page. you would use AJAX to send a request to the php page, the php page would access the database and send back a response, the javascript would then take the response and do whatever it needs with it. the response data is usually formatted in a JSON format, because PHP can easily create JSON, and javascript can easily decode JSON once it receives it as a response. to make using AJAX simpler, you may want to look in to using jQuery, a javascript library which can simplify the process.