我可以向同一台服务器发出 CURL 请求吗? [英] Can I do a CURL request to the same server?

查看:37
本文介绍了我可以向同一台服务器发出 CURL 请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一种方法来对位于同一服务器或另一台服务器上的页面进行 POST 调用.我们不能使用 include,因为我们调用的文件通常调用不同的数据库或具有相同名称的函数.

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.

我一直在尝试使用 curl 来实现这一点,虽然在从另一台服务器调用文件时它工作得很好,但在调用文件所在的同一台服务器时我绝对没有得到任何信息.

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!!";
?>

调用 File1.php 后,我什么也没有得到,但如果 File2.php 在另一台服务器中,我就会得到结果.有什么帮助吗?

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

我尝试同时使用服务器 URL (http...) 和文件的总地址 (/home/www....)

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

推荐答案

请注意,如果您向自己的站点发出 CURL 请求,则您使用的是默认会话处理程序,并且您请求的页面通过CURL 使用与生成请求的页面相同的会话,您将遇到死锁情况.

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 的脚本将锁定会话文件,并且 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 请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆