phpcakephpweborb

Passing parameters from external PHP service to CakePHP controller actions


Asking this question here because in CakePHP google group got no answer.I have the following pipeline: There is a CakePHP based application.It has a controller with an action called save_results. The action is a method accepting 3 arguments.So it looks like this:

function save_results($data,$rawdataset,$status){

 ///stub///
   return "all the data received";
}

Besides CakePHP app I have a WebOrb(PHP) which I use to communicate with Flash application using AMF format.The flash application sends data to WebOrb service which then sends it to the action I described above in Cake.So I receive from the Flash app 3 objects to match the params I pass into the cake method (save_results) : data -of type array of objects. rawdataset-of type string containing xml object. status-unsigned integer 0 or 1. In the WebOrb service the data received from the Flash is ok.But when I try to send it further into CakePHP it doesn't pass through. I do it this way (In The WebOrb class):

public function sendToCake($data,$rawdataset,$status){


    $encodedData=urlencode($data);
    $encodedRawData=urlencode($rawdataset);

    $destinationURL="http://localhost/mycakeapp/resultscontroller/save_results  /".$encodedData."/". $encodedRawData."/".$status;

   return file_get_contents( $destinationURL);

}

I found that a parameter passed into url for cake can not have spaces between characters.SO I use urlencode().It works for simple strings like "Hello world".But once I pass more complex strings like xml strings ($rawdataset object) the cake doesn't receive such a call.I tried also rawurlencode() and still no results.Is it possible that cake doesn't know how to treat xml structured strings when passed via http call? Any help or advice on this issue will be highly appreciated.


Solution

  • For your WebOrb class, change it so it uses HttpRequest instead:

    public function sendToCake($data,$rawdataset,$status){
        $url = 'http://localhost/mycakeapp/resultscontroller/save_results';
        $r = new HttpRequest($url, HttpRequest::METH_GET);
        $r->addQueryData(array('data' => $data, 'rawdata' => $rawdata, 'status' => $status));
    
        try {
            $r->send();
            return $r->getResponseData();
        } catch (HttpException $ex) {
            return $ex;
        }    
    }
    

    ^^ You may have to tweak it around a bit to return exactly what you want. For your CakePHP Controller / Action, inside it you can access the get request via the Parameters Attribute.

    function save_results(){
        $data = $this->params[];
        //do stuff to my data;
        return $data;
    }
    

    Hope that helps!