发送带有CURL的HTTP请求到本地文件 [英] send http request with CURL to local file

查看:623
本文介绍了发送带有CURL的HTTP请求到本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

hello i need to send http request to a local file using the file name it self not the full http path for example

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

我需要这样做才能变得像这样

i need to do it to become like this

<?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或任何其他方法的解决方案?
谢谢

when i tried to do the second example it doesn't make anything. is there any solution using the Curl or even any other method ? thank you

推荐答案

从PHP 5.4.0开始,CLI SAPI提供了内置的Web服务器。您只需提供fe.php脚本所在的目录,然后运行将其卷曲的脚本即可。

As of PHP 5.4.0, the CLI SAPI provides a built-in web server. You could simply serve up the directory that your fe.php script lives in and then run your script which curls this.

从命令行更改为目录并运行:

From the command line change to the directory and run:

cd /path/to/script/te/
php -S localhost:8000

现在修改代码,使其在端口8000上连接:

Now amend your code so that it connects on port 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

这篇关于发送带有CURL的HTTP请求到本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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