php 我可以向同一台服务器发出 CURL 请求吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5412069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I do a CURL request to the same server?
提问by Trialien
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.
我需要实现一种方法来对位于同一台服务器或另一台服务器上的页面进行 POST 调用。我们不能使用 include,因为我们调用的文件通常调用不同的数据库或具有相同名称的函数。
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.
我一直在尝试使用 curl 来实现这一点,虽然它在从另一台服务器调用文件时工作得很好,但在调用文件所在的同一台服务器时我绝对没有得到任何东西。
EDIT TO ADD SOME CODE: A simplified version of what I'm doing:
编辑添加一些代码:我正在做的简化版本:
File1.php
文件1.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
文件2.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?
调用 File1.php 后,我什么也没得到,但是如果 File2.php 在另一台服务器中,那么我会得到结果。有什么帮助吗?
I tried using both the server URL (http...) and the total address of the files (/home/wwww....)
我尝试同时使用服务器 URL (http...) 和文件的总地址 (/home/www....)
回答by Marc B
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.
请注意,如果您向自己的站点发出 CURL 请求,则您使用的是默认会话处理程序,并且您通过 CURL 请求的页面使用与生成请求的页面相同的会话,您将运行陷入僵局。
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.
默认会话处理程序在页面请求期间锁定会话文件。当您尝试使用同一会话请求另一个页面时,该后续请求将挂起,直到请求超时或会话文件可用。由于您正在执行内部 CURL,因此运行 CURL 的脚本将锁定会话文件,并且 CURL 请求永远无法完成,因为目标页面永远无法加载会话。
回答by Kevin Nguyen
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:
因为当您尝试使用公共 ip 请求本地服务器时,apache 无法解析到其本地域。因此,您必须检查该域使用的是哪个本地 ip apache。然后您需要编辑 /etc/hosts 文件并添加带有本地 ip 和您的域的新行。例如:
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:
我在 apache 虚拟主机中该域的本地 ip 是:172.190.1.120,我的域是 mydomain.com 所以我将添加:
172.190.1.120 mydomain.com
172.190.1.120 mydomain.com
Then your curl will work properly.
然后你的卷曲将正常工作。
回答by vladh
You should refactor your code. In addition to what Marc B mentioned, this approach will unnecessarily slow down your script (potentially by a large margin) and cause lots of confusion. No offense, but this is just an incredibly hacky fix for bad logic.
你应该重构你的代码。除了 Marc B 提到的内容之外,这种方法会不必要地减慢您的脚本速度(可能会大幅降低)并导致很多混乱。无意冒犯,但这只是针对糟糕逻辑的令人难以置信的黑客修复。