Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do a CURL request to the same server?

Tags:

php

curl

I need to implement a way to make POST calls to pages located on the same server or in another server. We cannot use include because the files that we are calling usually call different databases or have functions with the same name.

I've been trying to implement this using curl, and while it works perfectly when calling files from another server, I get absolutely nothing when making a call to the same server where the file is.

EDIT TO ADD SOME CODE: A simplified version of what I'm doing:

File1.php

<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "www.myserver.com/File2.php"); curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_VERBOSE, true); $result = curl_exec($ch); curl_close($ch); echo $result; ?> 

File2.php

<?php echo "I'M IN!!"; ?> 

After calling File1.php, I get nothing, but if File2.php is in another server then I get a result. Any help?

I tried using both the server URL (http...) and the total address of the files (/home/wwww....)

like image 673
Trialien Avatar asked Mar 23 '11 21:03

Trialien


People also ask

Does curl have a limit?

From the security standpoint it isn't particularly useful to restrict outgoing data and, as far as I know, neither the Curl library nor PHP itself impose any limit.

Does curl use HTTP?

cURL supports several different protocols, including HTTP and HTTPS, and runs on almost every platform.

What protocol does curl use?

curl is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, GOPHER, DICT, TELNET, LDAP or FILE). The command is designed to work without user interaction.


2 Answers

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.

like image 122
Marc B Avatar answered Sep 28 '22 13:09

Marc B


Because when you tried to request to the local server with the public ip, apache couldn't resolve to its local domain. So you have to check which local ip apache is using for that domain. Then you need to edit the /etc/hosts file and add the new row with local ip plus your domain. For example:

My Local ip for that domain in apache's virtual host is : 172.190.1.120 and my domain is mydomain.com So I will add:

172.190.1.120 mydomain.com

Then your curl will work properly.

like image 24
Kevin Nguyen Avatar answered Sep 28 '22 14:09

Kevin Nguyen