I've got an app engine app set up. It's set up with billing and I have extension="curl.so" in my php.ini file so I think cUrl should work. It is set up to allow cross domain requests.
I'm attempting to send data (via a jquery ajax post) to the app, then have it use curl to send the data somewhere else.
If I send no data with the ajax request, the curl works and the final destination responds telling app engine that there's no data, app engine successfully passes it back to the ajax callback (as expected).
However if I send data along, the app engine responds with a 500 error. It appears to be breaking during the curl_exec.
Everything works fine if I run it through the local server.
Does anyone have any idea what might be inhibiting app engine from allowing this?
Related
I have run into a strange situation in a Flutter Web application. I am making a request to a PHP script which appears to give me a CORS error, but it seems that the PHP code gets executed anyway...
When I make the request, the Flutter Web app throws an exception
XMLHttpRequest error
and the Chrome developer tools network tab shows CORS error in the status column for the request.
But despite this, it seems like the script is actually executed and inserting data into the database.
Is it possible that a PHP script can execute while still giving a CORS error on the client?
You need to check the http method in your script before executing your code :
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
}
The browser make a OPTIONS request to the server to check cors, which will execute your script, and if the cors check pass, your script will be called twice, one for the OPTIONS call, and another for the GET call
So here is my understanding of what's happening.
The web app makes a GET request to the server.
Since the app is running in a browser, the browser handles the request for the app.
2.1. The browser adds an Origin header to the request, with the value set to the current domain the web app is running on.
2.2 The browser sends the request to the server.
The server receives the request and relays it to the PHP script.
The PHP script can decide wether to
4.1 a) inspect the Origin request header and set an Access-Control-Allow-Origin response header that includes or excludes the Origin domain, or
4.1 b) just execute normally without checking the Origin request header or setting the Access-Control-Allow-Origin response header.
4.2 It's up to the PHP script do decide wether or not to execute, and wether or not to return any data.
The PHP script has finished executing and the web server sends the response back to the client.
The browser inspects the Access-Control-Allow-Origin header it got back from the PHP script.
6.1 a) If it includes the Origin domain (or is a wildcard *), the browser relays back the contents of the response back to the app.
6.1 b) Otherwise, the browser enforces the "same origin principle" and sends a "CORS error" back to the web app.
So I think my initial confusion was that the server wouldn't execute the request at all if the CORS headers weren't set. But what's actually happening is just that everything executes normally, the PHP script inserts some data into the database and sends a response, but when the browser detects that the response doesn't contain a CORS header that allows the Origin domain to read the response, it doesn't let the app see the response (even though the browser has already received it and it might contain a perfectly normal JSON/HTML/whatever).
The other answers talk about preflight OPTIONS requests, which do not seem to take place at all in this case. The Mozilla CORS documentation explains that "simple requests" don't trigger a CORS preflight.
does this API just not work via cross origin resource? My CORS headers are set in .htaccess and verified working, because I do get a response from the API, but it's an unknown error.
I set everything up and tested on a single test server and it all works. But my prod environment requires two separate domains, one which is a CMS where the pages are hosted, and then a separate PHP server where I'm hosting the PHP side of the app that makes the CURL requests. When I try to make a CORS request, I get
{"error":{"code":500000,"message":"An unexpected error occurred. If the error persists, report it with date/time of error, request identifier from response header X-RequestId, and client identifier from request header X-ClientTraceId."}}
I get the same thing when I mimic a CORS scenario locally by using a XAMPP PHP server and a Gulp HTTP server.
My setup currently builds the JSON from body copy on a page, then sends that via AJAX POST to the PHP file, which then processes it, encodes, makes Curl requests to the API, then outputs the response. I then handle the data from the response again in the JS file.
I have a website that is using jQuery's $.post method to submit a contact form to a PHP script that processes the info and sends it via email. I have tested the script locally and it's working properly. When I try to run it on the server, the ajax request to the processing script sits as (pending) for ~3 minutes before returning a status of 404. However if I inspect the request and navigate directly to the Request URL I can see that the script is indeed there and functioning properly.
The most bizarre thing, though, is that it returns an nginx 404 error. The server is running Apache, not nginx. Also, the site has a custom 404 page that works properly if you navigate to any non-existent page.
As a first test, I created another PHP script that simply echoes a string, put it in the same location as the form processing script, and AJAX requests to that work just fine. It's only the request to the processing script that doesn't go through.
You can view the page here with the contact form and AJAX Test. http://dev.alpha1marketing.com/contact/ When you submit the contact form a loader will spin indefinitely and if you inspect the request you'll see it pending for a few minutes before returning the mysterious 404. The Ajax Test button returns the request almost immediately.
Here is the code that posts the form submission to the form processing (this doesn't work)
# Post the form
$.post('/wp-content/themes/alpha1/ajax/send-contact.php', {
firstName: $.trim($('#firstName').val()),
lastName: $.trim($('#lastName').val()),
email: $.trim($('#emailAdress').val()),
referral: $.trim($('#referral').val()),
comment: $.trim($('#comment').val())
},
(response)->
# Show some messages
return
)
And here is the code that posts to the "test" script (this does work)
$.post('/wp-content/themes/alpha1/ajax/test.php', {
}, (response)->
console.log response
return
)
I'm fairly certain this has something to do with the server/network configuration, but I'm stumped as to what aspect of it. There's another site being developed in parallel you can view at http://dev.krasdalefoods.com/contact/, and it's having the same issue using the same form processing code.
The two dev. domains are hosted on Bluehost, and the plan is for the primary domains to move over to Bluehost once the sites are complete. The client purchased and initially configured some aspects of the server before giving me access to the control panel to set up what I needed so it's possible that something they did is causing this issue, but I'm not sure what it could be. I believe the primary www.alpha1marketing.com and www.krasdalefoods.com domains are hosted on IIS, so that couldn't be causing the nginx error either.
Any help debugging this would be appreciated!
Turns out that the issue with nginx was a red herring. The problem was the fact that BlueHost doesn't allow you to send messages through an external SMTP server - only through theirs. I was trying to send the messages through Gmail and that request was being blocked by BlueHost, which was causing their nginx middleware to see nothing but a hung script and return a 404.
Switching my mailer script to use PHP mail() was, unfortunately, the way I had to go. It still works!
I'm trying to write a Flask application that incorporates a PHP script via a POST request using the requests module. I've had success on my local machine where I post
requests.post("http://localhost/webapp/templates/script.php", data=blah)
and everything goes as expected. However, when I push my code to Heroku, the post request no longer works - instead, I'm redirected to
http://mysite/php_post
and get a
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
I guess you want to be posting the data to your heroku application
requests.post("http://localhost/webapp/templates/script.php", data=blah)
should be
requests.post("http://mysite/webapp/templates/script.php", data=blah)
I am developing an iPhone application that sends through HTTP requests an XML file to my PHP server.
I have coded my server and got the XML file and stored it in database. Now I want to see from my iPhone if the server got my XML file.
This response of the server, I must code it or is it automatic?
If it is not automatic, how do I make it? I must make the server send back a simple XML to my iPhone stating that everything is "OK" and also program my iPhone application to get the XML?
Assuming you have set up some proper API for your App to query, just use HTTP Status Codes. If everything was ok and the data got successfully inserted, reply with a 200 response. If there was an error there are numerous other status codes to send, e.g. 500 internal server error. Your client App can then parse these responses and react appropietly.