你好,我需要发送 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);
?>
我需要这样做才能变成这样
<?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);
?>
当我尝试执行第二个示例时,它什么也没做。 有没有使用 Curl 或什至任何其他方法的解决方案? 谢谢
最佳答案
从 PHP 5.4.0 开始,CLI SAPI 提供了一个内置的网络服务器。你可以简单地提供你的 fe.php 脚本所在的目录,然后运行你的脚本来 curl 它。
从命令行切换到目录并运行:
cd /path/to/script/te/
php -S localhost:8000
现在修改您的代码,使其连接到端口 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);
?>
现在运行你的脚本,例如:
php -f /path/to/curl/script.php
关于php - 使用 CURL 将 http 请求发送到本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6560512/