php - symfony 4 正在创建发布请求

我想创建函数来执行其他 url(此 url 在服务器上生成文件)并获得响应,然后向用户发送消息,但我不想使用 ajax,我尝试使用 Reqeust 对象,但它没有工作。

public function testCreateFile() {

    $uri = 'http://someuri/somefuncition';
    $method = 'POST';
    $paramaters = array(
        'csv' => '20190102212655-product-test.csv',
        'type' => '5', 

    );
   $request = new Request();
   $request->create($uri,$method,$paramaters);


    return new Response("Message to user") ;
}

我应该如何正确地做?

提前感谢您的帮助。

最佳答案

您需要的是curl。 最好的方法是使用下面的代码作为服务,所以我会给出完整的类

服务类

<?php
    App\Services;

    class PostRequest
    {
        static function get_data(String $url, Array $post_parameters)
        {
            //url-ify the data for the POST
            $parameters_string = "";

            foreach($post_parameters as $key=>$value) {
                 $parameters .= $key.'='.$value.'&'; 
            }

            rtrim($fields_string, '&');

            //open connection
            $ch = curl_init();

            //set the url, number of POST vars, POST data
            curl_setopt($ch,CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch,CURLOPT_POST, count($post_parameters));
            curl_setopt($ch,CURLOPT_POSTFIELDS, $parameters);

            //execute post
            $result = curl_exec($ch);

            curl_close($ch);

            return $result;
        }
    }
?>

因此,在您的 Controller 中包含 use App\Services\PostRequest; 之后,您可以执行此操作

public function testCreateFile() {
    $uri = 'http://someuri/somefuncition';
    $paramaters = [
        'csv' => '20190102212655-product-test.csv',
        'type' => '5',
    ];
    $request = PostRequest::get_data($uri, $paramaters);
    return new Response("Message to user") ;
}

https://stackoverflow.com/questions/54149893/

相关文章:

aws-lambda - AWS Lambda Go : The VM errors out in

python - 需要帮助来模拟 xhr 请求

android - Cordova 插件文件路径 : Unable to resolve files

node.js - 为什么作为响应 SIGINT 信号的回调调用的方法在进程终止之前没有完全完成?

angular - NRWL 中的多个应用程序可以访问定义文件的位置

c# - Selenium Firefox 不添加扩展

javascript - Laravel/Vue.js项目中如何使用Vuesax上传组件?

android - 获取/返回由 Android Room 数据库生成的自动生成的 ID(作为主键)

python - Apache Beam python Bigquery 将流式插入更改为批量插入?

laravel - Vue 多选 laravel 提交表单