6

I have been searching all over stackoverflow and Google for a solution to my problem.

I have 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 receive back a JSON with all the details associated with that variable using POST. Now I have 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 the dataAction from the Project1 controller and gives me an output of whatever is echoed. 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 displaying inside dataAction:

public function dataAction(){

    var_dump($this->getRequest()->getParam('var-name'));

    var_dump($_POST);

    die();      

}

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

What am I missing??? I have frustrated myself searching on it since morning but got no solution.

Thanks in advance for response.

Stephan Weinhold
  • 1,623
  • 1
  • 28
  • 37
1291
  • 117
  • 1
  • 2
  • 6

1 Answers1

5

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 body is not a url-encoded string, instead it is just raw JSON.

Calling $this->getRequest()->getParam('foo') looks at the PHP superglobals $_GET and $_POST which will not contain any of the JSON parameters. The reason it will be empty is because PHP couldn't parse the POST data since it was JSON and not HTTP url-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 full code you can mess with.

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;
}
drew010
  • 68,777
  • 11
  • 134
  • 162
  • Glad to have a quick response. As you said I tried using that code but I got a NULL with var_dump($json) and bool(false) with var_dump($post); and I agree with your post json not being parsed. But when I try this: $client->setRawData($test, 'String')->request('POST'); where test is a string it still gives me nothing but NULL ? – 1291 Sep 24 '12 at 20:04
  • Not sure why that it didn't work for you. I added the full code for both controller actions in the code. It's mostly a copy/paste of what you had for the request. Hopefully you can get that working. Make sure you are using the latest ZF version 1.12.0. I tested with 1.11.13 and it worked with that version as well. – drew010 Sep 24 '12 at 20:32
  • Apparently I was not sending the data properly... this is what I changed: $data = array('num' => '1234'); $dataString = ""; ... While on the Server side I did this: $this->request->getParam('num') $this->json->sendData($data);.. and it now works :) ! Thanks for all the help ! Appreciate it! – 1291 Sep 25 '12 at 17:29
  • I love you. Thank you for finally giving me the answer that will let me go home after a 12 hour day... – rockstardev Jan 21 '13 at 19:52
  • Where could I put a custom port number for a POST? in the URL correct? $uri = 'http://playground:8080/zendapp/public/index/data'; – Erik Apr 04 '14 at 20:09
  • This solution did not help me, I created a new question here: https://stackoverflow.com/questions/50762118/ maybe anyone here would be able to help? – nclsvh Jun 08 '18 at 13:53