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

标签 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);
?>

我需要这样做才能变成这样

<?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/

相关文章:

c++ - c++ 类中的 curl 问题

curl - bash 脚本的一个小问题,使函数在初始函数调用后接受所有参数作为单个字符串

javascript - 警报消息框后面显示空白页

php - 重定向到单个搜索结果?

php - 如何在 PHP 中将 MongoDB BSONDocument 转换为有效的 JSON?

PHP Curl http 代码 100 失败

php - 如何使用Curl检查API请求超时?

php - 如何从 MySQL 表中提取多个 HTML 标签

php - 将 windows-1251 目录名称转换为 unicode(使用 Python)

mysql - 从外部主机获取动态数据 - 建立 mysql 连接或通过curl获取数据?