Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've built a plugin that uses AJAX to post a URL to the URL2PNG API and bring back a screenshot into WordPress via wp_remote_get. It works beautifully in my development environment. In production, though, it needs to go through a proxy.

Here's where it got sticky.

We defined the proxy via wp-config and it worked beautifully. In my testing I was able to successfully connect to the URL2PNG API via the proxy and return results..

..but..

When the request originated via AJAX, no success.

I've gone through extensive testing and here's what I've found:

  • Requests originating directly in plugin (e.g. on page load) are routed properly through the proxy without trouble.

  • Requests that start in the browser (e.g. via AJAX) fail to connect.

  • Closer inspection by server support showed that the request was attempting to go direct and not making use of the proxy.

Have any of you run into this before? Any ideas what may be going on?

share|improve this question
    
Please post some code examples so that we can see what you're doing. There's no real difference as to whether you're using ajax or not, if it's wp_remote_get doing it, then it will behave the same. –  Otto Jan 29 at 0:39
    
Ironically, a year later, I am working on the same project, though slightly different issue. It turned out that wp_remote_get simply wasn't using the proxy we set and, to make it work, we just used cURL directly. –  Jonathan Wold Jan 29 at 5:20
add comment

2 Answers

If your AJAX is a 'post', or for that matter any protocol, to URL2PNG the client is doing so directly. The client isn't aware of, nor would it honor you server's proxy configuration.

Considering the brief read of URL2PNG docs, placing your API key and password in your JavaScript/jQuery is risky at best since you'd be exposing that information to any site visitor willing to view the source of your page/document.

Perhaps you need to write a handler within WordPress and make your AJAX call directly to your site.

share|improve this answer
    
Thanks for the response! To be clear, I am using the "wp_ajax_" action hook to run wp_remote_get which, in theory, should mean that it's routing properly through the proxy. So, AJAX goes to function, which then goes out to URL2PNG. The problem is that it's not actually going through the proxy when the request originates through AJAX. –  Jonathan Wold Jan 22 '13 at 20:43
    
Review your 1) proxy configuration, 2) user context of PHP and Apache (which can be different) and of course 3) your own code. –  Stephen Jan 22 '13 at 21:00
    
Can you elaborate what you mean by "user context"? –  Jonathan Wold Jan 22 '13 at 21:01
    
Depending upon your server configuration Apache and PHP can be running as different users with different permissions and security contexts. So this can make a difference in how the two behave. –  Stephen Jan 22 '13 at 21:04
    
Gotcha.. This is an intense, high-security corporate server environment where I have no control over server settings. The server techs are investigating on their end to see if they can turn anything up - I just wanted to check and see if anyone else has run into this type of problem before. –  Jonathan Wold Jan 22 '13 at 21:10
show 1 more comment

You can have the client do the AJAX request to a PHP script hosted on your server and then have that script take over and do the request to the URL2PNG API... Other than that, I don't really see any way where you can enforce your proxy settings on a client's connection, since the AJAX request is triggered from the client's machine.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.