使用CURL将http请求发送到本地文件

Mar*_*rco 5 php curl

您好我需要使用文件名将http请求发送到本地文件,例如,它不是完整的http路径

    <?php
$url = 'http://localhost/te/fe.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
?>
Run Code Online (Sandbox Code Playgroud)

我需要做到这样

<?php
$url = 'fe.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
?>
Run Code Online (Sandbox Code Playgroud)

当我试图做第二个例子时,它没有做任何事情.有没有使用Curl甚至任何其他方法的解决方案?谢谢

Gre*_*Rox 5

从 PHP 5.4.0 开始,CLI SAPI 提供了一个内置的 Web 服务器。你可以简单地提供你的 fe.php 脚本所在的目录,然后运行你的脚本来卷曲它。

从命令行切换到目录并运行:

cd /path/to/script/te/
php -S localhost:8000
Run Code Online (Sandbox Code Playgroud)

现在修改您的代码,使其连接到端口 8000:

<?php
$url = 'http://localhost:8000/fe.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
?>
Run Code Online (Sandbox Code Playgroud)

现在运行你的脚本,例如:

php -f /path/to/curl/script.php
Run Code Online (Sandbox Code Playgroud)


Pru*_*rus 0

我已经成功地使用 file:// 调用来调用本地 URL。不幸的是,我希望能够使用相对路径,以便更容易在项目文件中维护,但您可以这样做:

在这里,我假设我正在使用 WAMP,但该解决方案也适用于服务器:** http://te/fe.php** 将转换为(假设它在 wamp 中)为: file:///c:/wamp/www/ te/fe.php