0

I am wondering if it is possible to use JS (jQuery) to make a $.post from any website on any domain, to a script on my server?

The reason for this question, is because I do not want to give out my PHP files to the client (and I dont want to spend money on ionCube or the likes), so making the request to my server would not give out my source.

My prefered way would be to simply include a JS file, like

<script src="http://MySite.com/MyScript.js"></script>

and have a function in there that does the Post to my PHP script, however due to the same-origin policy, I cannot do a POST request to a remote server.

I could of course make a proxy.php and make the request to it, but I am trying to keep it in a way so the client only have to include a small snippet into their HTML code, in order to use my app.

Is there a (less painfull) way to do this?

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Jeff
  • 12,085
  • 12
  • 82
  • 152
  • Don't do this. What if your client's visitors don't have javascript enabled, or use a jQuery-unsupported browser? Your remote PHP can't be called and the site is left non-functioning. – Michael Berkowski Jul 24 '11 at 13:42
  • This is a bit dated but [Cross-domain communications with JSONP...](http://www.ibm.com/developerworks/library/wa-aj-jsonp1/). – Roman Jul 24 '11 at 13:44
  • @Michael - The people who dont have Javascript enabled, or are running non-jQuery supported browsers, should not be using the Internet at all ;) - Most services I see just give you a – Jeff Jul 24 '11 at 13:44
  • @Jeff You must never have heard of "graceful degradation". – Michael Berkowski Jul 24 '11 at 13:45
  • @R0MAN - I am not sure I understand the concept – Jeff Jul 24 '11 at 13:52
  • @Jeff: Maybe people who don't understand `JSONP` shouldn't be doing web programming :P. – Roman Jul 24 '11 at 13:54
  • @R0MAN - Touché my friend. :P Honestly this is just for the back-end, my front-end will be a Windows Application, hahah ;) – Jeff Jul 24 '11 at 14:02

1 Answers1

1

Instead of using Javascript, why not give your client a PHP file that calls file_get_html() against your code on your server? It would have the same effect, without worrying about the client-side Javascript.

This would function similarly to a remote PHP include, but it is executed on your server and received as HTML.

You could also use cURL in PHP to call your remote PHP script, returning the output as HTML, and parsing out the bits you need.

file_get_html() is component of Simple HTML DOM

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • this would force the client to rename their site file into .php, and that is frustrating for many people. :) – Jeff Jul 24 '11 at 13:49
  • @Jeff that can be handled transparently with .htaccess by setting the .html handler to the PHP engine. A common solution. – Michael Berkowski Jul 24 '11 at 13:52
  • So if I wish to avoid using other frameworks (than jQuery, if necessary), I could use cURL inside a php snippet, that does the post request, and echo's the output, right? – Jeff Jul 24 '11 at 13:55
  • @Jeff Yes, that's correct. The PHP is then executed on your server and delivered as HTML to the cURL caller on the client's server – Michael Berkowski Jul 24 '11 at 13:57
  • I suppose that would be the best way. How would I do the htaccess modification you spoke about? – Jeff Jul 24 '11 at 13:59
  • @Jeff see http://stackoverflow.com/questions/6295141/server-not-parsing-html-as-php – Michael Berkowski Jul 24 '11 at 17:49
  • Is your method also usable when requesting info on Button Click? I dont do the POST before the user clicks a button, and I would like it to be AJAX, however I cant do a POST to a different domain. How can I use cURL for that? – Jeff Jul 24 '11 at 18:28
  • You can use AJAX if you put an AJAX handler on the client's server that executes the PHP which calls your server then passes the resulting HTML back to the browser. – Michael Berkowski Jul 24 '11 at 19:15
  • @Michael let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1783/discussion-between-jeff-and-michael) – Jeff Jul 24 '11 at 19:17