向Guzzle发送Form-Data Post请求



我正在使用guzlezz http client,并希望在使用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 );
                }
            },

我如何用guzzle制作邮政请求form-data,我尝试做类似的事情:

    $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'];
    }

但是,这是不起作用的,我该怎么做?

$http = new GuzzleHttpClient;
$response = $http->post('https://your-endpoint.com', [
    'form_params' => [
        'security' =>  $data['security'],
        'action'   =>  $data['action']
    ],
]);

最新更新