2

I'm using Zurmo and trying to create a new account using REST API. I followed this documentation precisely: http://zurmo.org/wiki/rest-api-specification-accounts to pass the required parameters as json array.

This is my php code:

public function actionCreateOrUpdate() {

     $params=$_POST;
    $modelClassName=$this->getModelName();

    foreach ($params as $param)

    {
        if (!isset($param))
        {
            $message = Zurmo::t('ZurmoModule', 'Please provide data.');
            throw new ApiException($message);
        }

            $r=$this->GetParam($param);

            $res= array('status' => 'SUCCESS',  'data' => array($r));


            print_r(json_encode($res,true));

     }

    }     

 function GetParam ($param){
        $modelClassName=$this->getModelName();



        if (isset($param['mobile_id'] ) && !$param['mobile_id']=='' &&!$param['mobile_id']==null)
        {   $id=$param['mobile_id'];

        $params=array();

            foreach ($param as $k => $v) {

                if(!($k=='mobile_id')) {

                    $params[$k] = $v;}



            }
            if ($params=null){$message = Zurmo::t('ZurmoModule', 'Please provide data.');
            throw new ApiException($message);}

        $tableName = $modelClassName::getTableName();
        $beans = ZurmoRedBean::find($tableName, "mobile_id = '$id'");

        if (count($beans) > 0)
        {
            $result    =  $this->processUpdate($id, $params);
        }else{
            $result    =  $this->processCreate($params,$id);
        }
    }

          return $result;

    }    

The problem is that the $_POST method is returning an empty array. While debugging I tried to use print_r($_POST) and it also returned an empty array. I also tried to pass parameters as plain text and got the same result. I tried $_GET method and it worked. I think the problem is in the $_POST method, maybe I need to change something in my .php files. Any ideas please?

2
  • Can you show the request? If $_GET is working and not $_POST you might be missing the request type when you send it. Commented Jun 8, 2014 at 14:57
  • the request type is REST. I also tried to use the HTTPRequester but it gave the same result :( Commented Jun 8, 2014 at 19:23

2 Answers 2

1

You should first hit the api with static data, to check if it works fine, then try to integrate php within that static data. You will need to read the documentation for which action accepts which format, and which method is supported(GET OR POST). Then try die(); , before sending if the array formed is as per the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually I tried hitting the api with static data and it worked. And the method I'm implementing supports POST. Thanks for your advice.
0

I had similar issue when creating Account using REST API from java client. The problem was I did not send the proper POST request. Another symptom also was on server side print_r(file_get_contents("php://input"),true); php code returned the correct request data.

Specifically the root cause was:

  • The Content-Type HTTP header was not "application/x-www-form-urlencoded"

  • The value field value in POST request was not properly encoded ( I used java.net.URLEncoder.encode method to overcome this)

After fixing these it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.