Symfony - 如何执行另一个 php 脚本并获取结果?

发布于 2025-01-02 17:32:54 字数 899 浏览 4 评论 0原文

我有一个脚本,它生成一些图形并返回它作为结果,它还缓存这个图形等。我

使用 symfony2 并且在控制器中我需要调用这个脚本,现在我使用这个函数来调用我的 php 脚本

        private function http_post($url, $data)
            {
                $data_url = http_build_query ($data);
                $data_len = strlen ($data_url);

                return array ('content'=>file_get_contents ($url, false, stream_context_create (array ('http'=>array ('method'=>'POST'
                        , 'header'=>"Connection: close\r\nContent-Length: $data_len\r\nContent-Type: application/x-www-form-urlencoded\r\n"
                        , 'content'=>$data_url
                        ))))
                    , 'headers'=>$http_response_header
                    );
            } 

:认为这种方式不是最好的,而且我记得 file_get_contents 非常慢? 所以我的问题是:通过“http_post”将 POST 发送到该脚本是好方法吗?如果没有的话,那什么会更好呢?

编辑:我不想在控制器中包含此脚本,因此请不要包含解决方案:)。

I have got script which generates some graphic and returns it as a result, also it caches this graphics etc.

I use symfony2 and in a controller I need to invoke this script, for now I use this function, to invoke my php script:

        private function http_post($url, $data)
            {
                $data_url = http_build_query ($data);
                $data_len = strlen ($data_url);

                return array ('content'=>file_get_contents ($url, false, stream_context_create (array ('http'=>array ('method'=>'POST'
                        , 'header'=>"Connection: close\r\nContent-Length: $data_len\r\nContent-Type: application/x-www-form-urlencoded\r\n"
                        , 'content'=>$data_url
                        ))))
                    , 'headers'=>$http_response_header
                    );
            } 

I think this way isn't the best and as I remember well file_get_contents is pretty slow?
So my question: is it good way to send POST to this script via "http_post"? If not, than what would be better?

EDIT: I don't want to have this script in a controller, so no include solutions please :).

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

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

发布评论

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

评论(2

李不 2025-01-09 17:32:54

如果您希望将图像生成器实现为控制器,则只需照常返回响应:return new Response($ generated_image);

所以这可能是您的控制器:

namespace Acme\MyBundle\Controller;
use Symfony\Component\HttpFoundation\Response;

class ImageGeneratorController
{
    public function generateAction($parameters)
    {
      //Generate an image using parameters and store it in $image
      $image = ....

      return new Response($image);
    }
}

然后,您可以从任何控制器调用图像生成器< /a> 使用 forward()

public function indexActionInAnotherController($name)
{
    $response = $this->forward('AcmeMyBundle:ImageGenerator:generate', array(
        'name'  => $name,
        'color' => 'green'
    ));

    // further modify the response or return it directly

    return $response;
}

If you wish to implement the image generator as a controller, you would just return the response as normal: return new Response($generated_image);.

So this could be your controller:

namespace Acme\MyBundle\Controller;
use Symfony\Component\HttpFoundation\Response;

class ImageGeneratorController
{
    public function generateAction($parameters)
    {
      //Generate an image using parameters and store it in $image
      $image = ....

      return new Response($image);
    }
}

Then, you can call your image generator from any controller using forward()

public function indexActionInAnotherController($name)
{
    $response = $this->forward('AcmeMyBundle:ImageGenerator:generate', array(
        'name'  => $name,
        'color' => 'green'
    ));

    // further modify the response or return it directly

    return $response;
}
云淡风轻 2025-01-09 17:32:54

您所做的事情没有任何问题,但是如果您想探索替代方案,很多人都会使用快速且强大的curl 库。

There is nothing wrong with what you're doing, however if you want to explore an alternative, a lot of people use the curl library, which is fast and robust.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文