使用 cURL,(如何)可以在远程页面上执行 PHP 命令并将其作为字符串变量返回?

发布于 2024-11-03 02:42:19 字数 702 浏览 3 评论 0原文

我想使用 cURL 执行以下操作:

  1. 登录到我的 Wordpress 站点,执行我自己的 PHP 代码并将其保存到变量中:

    $code_to_execute_remotely = wp_create_nonce('my_form');

    $ch =curl_init();
    curl_setopt($ch, CURLOPT_URL, 'www.mywordpresssite.com');
    curl_setopt($ch, CURLOPT_SOMEMYSTERIOUSFUNCTION, $code_to_execute_remotely); $resulting_variable = curl_exec($ch);

  2. 然后在第二次 cURL 执行中使用该变量:

    curl_setopt($ch, CURLOPT_MYSECONDFUNCTION, $resulting_variable);
    $second_ececution = curl_exec($ch);
    curl_close($ch);

当然,我使用了虚拟代码来简化事情。其他功能(登录远程站点等)没有问题,只需远程执行该代码并将其结果作为可用变量返回,这就是我所需要的。由于我对 cURL 不太熟悉,因此我可能正在尝试超出其范围的东西,在这种情况下,我将非常感激知道什么替代方案可以实现这一目标。

What I want to do using cURL:

  1. Login to my Wordpress site, execute my own PHP code and save it to a variable:

    $code_to_execute_remotely = wp_create_nonce('my_form');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'www.mywordpresssite.com');
    curl_setopt($ch, CURLOPT_SOMEMYSTERIOUSFUNCTION, $code_to_execute_remotely);
    $resulting_variable = curl_exec($ch);

  2. Then use that variable in a 2nd cURL execution:

    curl_setopt($ch, CURLOPT_MYSECONDFUNCTION, $resulting_variable);
    $second_ececution = curl_exec($ch);
    curl_close($ch);

I've used dummy code to simplify things of course. The other functionality (logging into the remote site, etc.) is no problem, just executing that code remotely and returning its results as a usable variable, that's all I need. Since I'm not terribly familiar w/ cURL, it's possible I'm attempting something beyond its bounds, in which case I'd be very thankful to know what alternative could achieve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2024-11-10 02:42:19

在远程站点上使用 php eval 函数:

http://php.net/manual/en /function.eval.php

编辑完整说明:

在本地服务器上:

$post_data['curl_function'] = 'print wp_create_nonce("my_form");'; 
$post_string = http_build_query($post_data); 
curl_setopt($ch, CURLOPT_URL, 'www.mywordpresssite.com/receiver.php');
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch); //This will contain whatever receiver.php prints

在receiver.php 中的远程服务器上:

print eval($_POST['curl_function']);

注意:

  • 只有当您可以访问远程服务器 www.mywordpresssite.com 并上传receive.php
  • 这对于远程服务器来说非常不安全如果您不清理输入并有办法进行检查,以便只有您的本地服务器可以发送此请求。
  • 但话又说回来,如果您可以访问远程服务器,当您可以直接在receiver.php 中编写功能时,为什么还要通过 cURl 发送代码呢?
  • 因此,如果您无法访问远程服务器,那么答案是:不可以,如果您想访问远程服务器并向其上传文件,则无法通过另一台服务器上的 cURL 运行代码。

On the remote site use the php eval function:

http://php.net/manual/en/function.eval.php

Edit with full explanation:

On your local server:

$post_data['curl_function'] = 'print wp_create_nonce("my_form");'; 
$post_string = http_build_query($post_data); 
curl_setopt($ch, CURLOPT_URL, 'www.mywordpresssite.com/receiver.php');
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch); //This will contain whatever receiver.php prints

On the remote server in receiver.php:

print eval($_POST['curl_function']);

Notes:

  • This will only work if you can access the remote server www.mywordpresssite.com and upload the receiver.php
  • This is very unsafe for the remote server if you don't sanitize the input and have a way to check so that only your local server can send this request.
  • But then again if you can access the remote server why send code through cURl when you can just write the functionality directly in receiver.php?
  • So the if you can't access the remote server then the answer would be; No, you can't run code through cURL on another server if you want access the remote server and upload files to it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文