Symfony - 如何执行另一个 php 脚本并获取结果?
我有一个脚本,它生成一些图形并返回它作为结果,它还缓存这个图形等。我
使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望将图像生成器实现为控制器,则只需照常返回响应:
return new Response($ generated_image);
。所以这可能是您的控制器:
然后,您可以从任何控制器调用图像生成器< /a> 使用
forward()
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:
Then, you can call your image generator from any controller using
forward()
您所做的事情没有任何问题,但是如果您想探索替代方案,很多人都会使用快速且强大的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.