Zend_Http_Client POST 'missing required parame

2019-08-22 02:48发布

问题:

First of all, I looked at this question and tried the solution, did not work for me.

I am working in Zend 1.12 on a PHP 7.0.22 server.

I have a function that should post data to an API and receive a confirmation of the post.
Whenever I test it in my rest client, in firefox, I get this response from the API call:
"{\"code\":400,\"text\":\"Missing required parameters.\"}".
When I add a header: Content-Type: application/x-www-form-urlencoded I get the expected respone: '200, true'. But even when trying to set this header in my code explicitly, it still always returns 400.

Controller:

$params = array();
$params[ 'name' ] = 'John';
$params[ 'email' ] = 'john@example.com';

$client = new Zend_Http_Client();
$client->setUri( 'path/to/my/api' );
$client->setParameterPost( $params );
$response = client->request( Zend_Http_Client::POST );
var_dump( $response );

I have tried to work with raw data aswell..

$params = array();
$params[ 'name' ] = 'John';
$params[ 'email' ] = 'john@example.com';
$params = json_encode( $params );

$client = new Zend_Http_Client();
$client->setUri( 'path/to/my/api' );
$client->setRawData( $params, 'application/json' );
$response = client->request( Zend_Http_Client::POST );
var_dump( json_decode( $response ) );

When I try to dump the $request with the post parameters inside my API (becuase I wrote the API myself), I see that this is null. Like there is no $post being sent or no data is actually being passed. Because the error 'missing required parameter' is untrue, all the parameters are set ( but maybe not passed / sent correctly ).

Can somebody tell me what I am missing? I have been at this all day. Thanks!

EDIT (@shevron):
The API expects the parameters to be passed via url-encoded-form. Setting the appropriate header in the REST Client Content-Type: application/x-www-form-urlencoded makes the API return a good response. When this header is not added, I get the error.