How to Do a Curl Request to the Same Server

Can I do a CURL request to the same server?

Be aware that if you're issuing the CURL request to your own site, you're using the default session handler, and the page you're requesting via CURL uses the same session as the page that's generating the request, you'll run into a deadlock situation.

The default session handler locks the session file for the duration of the page request. When you try to request another page using the same session, that subsequent request will hang until the request times out or the session file becomes available. Since you're doing an internal CURL, the script running CURL will hold a lock on the session file, and the CURL request can never complete as the target page can never load the session.

PHP cURL request to same server returning false

Must be related to locking session situations. Try this way.

<?php
$url = 'http://api.some_address_on_the_same_server.com';
$postdata = array(...);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

session_write_close();

$result = curl_exec($ch); // $result is always false when on the same server for some reason
curl_close($ch);

session_start();
?>

EDIT :

Have you added an exception in your windows host file ?
/windows/system32/drivers/etc/hosts

like

127.0.0.1 yourdomain.com

For more information. Check out this

How to execute the same curl request on a sequence of server IPs (URLs)

You can either use shell's brace expansion in bash, e.g. {2..15} for 2,3,4,...,15:

curl ... http://x.x.x.{2..15}:8080/services/test

or, curl's alphanumeric series [] operator in the URL part (notice the double-quotes):

curl ... "http://x.x.x.[2-15]:8080/services/test"

Alternatively, you can use arithmetic for loop:

for ((i=2; i<=15; i++)); do
curl ... "http://x.x.x.$i:8080/services/test"
done

error: couldn't connect to host via CURL on same domain

Your updated code is not working because of the white space before http in $url = " http...

The original code is probably not working because of some firewall or nameserver issue. Try to visit the URL you want to load directly in a browser ON THE SAME SERVER. That will probably fail too.

You need to fix the server to accept requests from the localhost.

What can also help you on your way is to check the web server and firewall log files. See why the requests are being declined. And even temporarily just disable the firewall to rule it out.

How do I use cURL to perform multiple simultaneous requests?

While curl is a very useful and flexible tool, isn't intended for this type of use. There are other tools available which will let you make multiple concurrent requests to the same URL.

ab is a very simple yet effective tool of this type, which works for any web server (despite the introduction focusing on Apache server).

Grinder is a more sophisticated tool, which can let you specify many different URLs to use in a load test. This lets you mix requests for cheap and expensive pages, which may more closely resemble standard load for your website.



Related Topics



Leave a reply



Submit