dou4121 2017-12-12 15:31
浏览 448

用guzzle发送表单数据发布请求

I am using guzzle http client, and would like to implement the post request that looks like this when done with js:

        var formData = JSON.stringify( { id: formID, fields: fields, settings: settings, extra: extra } );
        var data = {
            'action': 'nf_ajax_submit',
            'security': nfFrontEnd.ajaxNonce,
            'formData': formData
        }

        var that = this;

        jQuery.ajax({
            url: nfFrontEnd.adminAjax,
            type: 'POST',
            data: data,
            cache: false,
            success: function( data, textStatus, jqXHR ) {
                try {
                    var response = jQuery.parseJSON( data );
                    nfRadio.channel( 'forms' ).trigger( 'submit:response', response, textStatus, jqXHR, formModel.get( 'id' ) );
                    nfRadio.channel( 'form-' + formModel.get( 'id' ) ).trigger( 'submit:response', response, textStatus, jqXHR );
                    jQuery( document ).trigger( 'nfFormSubmitResponse', { response: response, id: formModel.get( 'id' ) } );
                } catch( e ) {
                    console.log( e );
                    console.log( 'Parse Error' );
                    console.log( e );
                }

            },

How can I make a post request form-data with guzzle, I have tried with doing the same thing like this:

    $url = 'http://ytf.app/wp-admin/admin-ajax.php';
    $data = [];
    $data['security'] = $this->getNonce();
    $data['action'] = 'nf_ajax_submit';
    $data['formData'] = $request->all();
    $formData = json_encode($data);

    $client   = new Client();
    $response = $client->request('POST', $url, [
        'body' =>  $formData,
    ]);

    $body = json_decode($response->getBody(), true);

    if ($body['success'] === true && $body['data'] !== false) {
        return $body['data'];
    }

But, that is not working, how should I do this?

  • 写回答

1条回答 默认 最新

  • doucheng7808 2018-08-25 15:18
    关注
    $http = new GuzzleHttp\Client;
    
    $response = $http->post('https://your-endpoint.com', [
        'form_params' => [
            'security' =>  $data['security'],
            'action'   =>  $data['action']
        ],
    ]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿