Zend_HTTP_Client will not give me a POST of any data

I searched all of stackoverflow and google to solve my problem.

I created two projects with Zend Framework - Project1 and Project2 - and I want to implement web services on one of them. The idea is to send a JSON string to Project1 and get back JSON with all the information associated with this variable using POST. Now I created a TestController on Project2 :

 public function indexAction(){ $uri = 'http://project1.com/WebService/data'; $config = array( 'adapter' => 'Zend_Http_Client_Adapter_Curl', 'curloptions' => array(CURLOPT_FOLLOWLOCATION => true), ); $client = new Zend_Http_Client($uri, $config); $request = $client->request('POST'); print_r($request->getBody()); exit(); } 

The above code works. It reads dataAction from the dataAction controller and gives me the output of what the echo is. But when I try this:

 public function indexAction(){ $uri = 'http://project1.com/WebService/data'; $config = array( 'adapter' => 'Zend_Http_Client_Adapter_Curl', 'curloptions' => array(CURLOPT_FOLLOWLOCATION => true), ); $client = new Zend_Http_Client($uri, $config); $data = array( 'userID' => 'TEST TEST', 'value' => 1, 'description' => 'ABCDEFG', ); $request = $client->request('POST'); $json = json_encode($data); $client->setRawData($json, 'application/json')->request('POST'); exit(); } 

And on the server side, when I try to display inside the dataAction :

 public function dataAction(){ var_dump($this->getRequest()->getParam('var-name')); var_dump($_POST); die(); } 

I get the output of this: NULL array (0) {} .... I get the same result when I try it on the client side. Also mention .. I also tried to open the php: // file, but got an empty line ...

What am I missing ??? I disappointed myself in the search in the morning, but did not get a solution.

Thanks in advance for your reply.

+6
source share
1 answer

Here is what you are missing:

 $json = json_encode($data); $client->setRawData($json, 'application/json')->request('POST'); 

sends a POST request, but the data in the POST module is not a url encoded string, instead it is just raw JSON.

The call to $this->getRequest()->getParam('foo') considers the PHP superglobals $_GET and $_POST that will not contain any JSON parameters. The reason it will be empty is because PHP was unable to parse the POST data, as it was JSON, not HTTP-encoded content.

The solution is to use something like this in the dataAction if you want to receive JSON data in the POST body.

 $post = $this->getRequest()->getRawBody(); try { $json = Zend_Json::decode($post); // now access parameters from $json array } catch (Zend_Json_Exception $ex) { echo "Failed to decode request, POST did not contain valid JSON."; } 

Edit: Here is the complete code you may come across.

 public function requestAction() { // CHANGE THIS $uri = 'http://playground/zendapp/public/index/data'; $config = array( 'adapter' => 'Zend_Http_Client_Adapter_Curl', 'curloptions' => array(CURLOPT_FOLLOWLOCATION => true), ); $client = new Zend_Http_Client($uri, $config); $data = array( 'userID' => 'TEST TEST', 'value' => 1, 'description' => 'ABCDEFG', ); $json = json_encode($data); $resp = $client->setRawData($json, 'application/json')->request('POST'); var_dump($resp->getBody()); exit(); } public function dataAction() { $post = $this->getRequest()->getRawBody(); try { $json = Zend_Json::decode($post); print_r($json); } catch (Exception $ex) { echo "failed to decode json"; } exit; } 
+5
source

Source: https://habr.com/ru/post/926143/


All Articles