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?

7
  • 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. Commented Jul 24, 2011 at 13:42
  • This is a bit dated but Cross-domain communications with JSONP.... Commented Jul 24, 2011 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 <script> tag to insert wherever you want their stuff to appear, so why cant I? :) Commented Jul 24, 2011 at 13:44
  • @Jeff You must never have heard of "graceful degradation". Commented Jul 24, 2011 at 13:45
  • @R0MAN - I am not sure I understand the concept Commented Jul 24, 2011 at 13:52

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

9 Comments

this would force the client to rename their site file into .php, and that is frustrating for many people. :)
@Jeff that can be handled transparently with .htaccess by setting the .html handler to the PHP engine. A common solution.
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 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
I suppose that would be the best way. How would I do the htaccess modification you spoke about?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.