Asynchronous cross-domain GET requests - php

Does anyone has any solutions on accomplishing asynchronous cross-domain GET requests. I am looking to make a site that checks available names of other sites. The faster the better.
I'd like it to use my server if possible, as its most likely faster than the client. Would most likely send it a huge array (300-10000) requests.
Examples, links, anything will work.

You would have to make a same-domain get request to your server, and have your PHP script do the checking (maybe using CURL) before responding to the request.
http://www.php.net/manual/en/curl.examples-basic.php

Do you want to perform the Cross-Domain Check using JavaScript or using PHP?
If using JavaScript you will probably be restricted by the Same-Origin Policy, though some pages may allow your browser to access them using Ajax.
If using PHP there is no way to perform a asynchronous request, because PHP is synchronous all over.
Maybe a good variant would be to send a request to a Node.JS server from your JavaScript and then let Node.JS get the page without blocking a process?

Check out curl http://us.php.net/curl

Related

Sending data to a slow API without slowing down page load

I am doing a curl call to pass information to an API. The issue I am having is sometimes the API responds slowly. I need to immediately pass the data, but I don't want the user to be stuck on the processing page while the API tries to make the connection.
Is there a good alternative, kind of like multithreading or something that I could use to still query this API while moving the user onto the next page?
Thanks!
Use fire-and-forget.
I don't know if CURL can do this but make it so it wont attempt to read from the stream. Just send and close.
If the connection to the remote site is slow as well you'll need to do some proxying.
Fire-and-forget proxying is a poor man's solution to threading.

Fastest way to Fire and Forget a JSON POST in PHP

I'm currently in the process of building/implementing a logging system for a website I'm working on that's in PHP. The way the logging system works is I send a JSON request to localhost and that json gets logged (basically, anyway.
My question is:
what's the fastest way I can make a quick fire and forget call with a JSON POST? Is there a way to fire and forget with cURL?
There are multiple ways to do it: you could use the curl_multi functionality of the php_curl extension, which allows you to send asynchronous HTTP requests using cURL, but this requires that extension. GuzzlePHP provides a large wrapper around much of the functionality of cURL, including the curl_multi features if you are looking for an object-oriented approach. PHP's sockets also support asynchronous communications, a library which implements this for the HTTP protocol is available here [the client is written in "pure" PHP and has no dependency on cURL but supports asynchronous requests and fully complies with the HTTP 1.1 spec].
If you are looking for a fire and forget logging solution you might want to look at something that uses UDP protocol like Graylog.
You could use a small image that hits a PHP script. The php script logs the hit and returns a tiny 1x1 transparent GIF. Then the logging will happen after the page loads.

Keep-alive in curl / php

I'm writing a gateway script in PHP which connects to a remote server, obtains some information and returns it for JSON usage (no JSONP possibility).
This gateway is being requested every second, so it's very important for curl to use keep-alive. From what I learned, curl will do it automatically if we will use the same handle across multiple requests.
The question is: how do I store the handle between two reloads? It's not possible to store the handle resource in session, it also can't be serialized.
Or maybe there's other way to ensure keep-alive in curl?
Generally speaking, every request exists independent of every other request. Connections and other resources are not pooled between requests.
There are possible solutions
Use a proxy with content adaptation (Squid and Greasyspoon would work here) this does take some work to set up. But you will be able to write scripts in java, javascript or ruby to adapt your content.
Run your PHP script as a deamon, sort of like a webserver. This would take a bit of engineering, but it can be done with PHP. You would be getting into sockets and threading.
You might be able to use this as a starting point: http://nanoweb.si.kz/

Asynchronous HTTP request in PHP with delegate

how can I make an asynchronous HTTP request in PHP passing a delegate to the function?
Thanks
PHP doesn't really support async callbacks since there are no interrupts in the language. Your best of using curl, and then checking back on the request to see if it has finished. You can also use fsockopen and friends to do it on a socket level.
Check out this post. You'll need to tweak it to save the result socket (and process it), but the basic idea is there.

How to use jQuery AJAX for an outside domain?

I am trying to perform a simple jQuery AJAX attempt using either a .get() or a .post().
If I have a local copy on my server and do:
$.get('/hash.php',...,...)
I monitor in my console in firebug that the get is done and I get a response.
All I change is the URI to an outside server and nothing happens.
$.get('https://secure.mysite.com/subdir/hash.php',...,...)
Doesn't help if I take the 's' off or if I use post instead. Am I missing some parameter that I should use in jQuery?
EDIT: I forgot to mention the reason I'm doing this is because I am eventually migrating from a PHP4 site to a PHP5 site, but for now the live PHP4 site needs a function that isn't in PHP4. So I am calling a PHP5 server to do it. I think I have a good workaround. Thanks!
You cannot send an Ajax Request to another domain than the other on which your application is deployed. This is because of the Same Origin Policy implemented in web-browers -- a security measure.
There are two possible solutions, though :
sending the request to your own server, that will act as a proxy to another (either via a PHP script, or, better, using some of Apache's mod_proxy_http module)
or not using "Ajax", but other techniques, like dynamically creating <script> tags -- which are not subject to the SOP constraint.
It's true that you normally can't do Ajax outside your domain due to the browsers. However using JSONP it is possible to do this. jQuery also has a jsonp param for Ajax now. To make this work you need to control the output of the server though.
Javascript cannot access a server outside of where the javascript file came from.
That is a security feature.
Depending on how browser-specific you want to get you may get around this, but that becomes a bit of a slippery slope.
You cannot do cross domain ajax requests directly, this would be a security concern.
You will need to call your local php file from jquery and have the php file talk to the other domain.
There's a method called JSONP which is used to circumvent that. See the 2nd reply on SO #570100

Categories