0
<?php
require_once 'Zend/Session/Namespace.php';
class ApiController extends Zend_Rest_Controller
    {
     public function init()
     {
     $this->_helper->layout->disableLayout();
     $this->_helper->viewRenderer->setNoRender(true);
     }
  public function indexAction()
     {
      $query=$this->getRequest()->getParam('query');
      $this->getResponse()
      ->appendBody("hi");
      $this->getResponse()->setHttpResponseCode(200);
     }
 public function getAction()
     {
     $query=$this->getRequest()->getParam('id');
     $this->getResponse()
          ->appendBody($query);
     }
 public function postAction()
     {
      $this->getResponse()
           ->setHttpResponseCode(200)
           ->appendBody("From postAction() creating the requested article");
     }
 public function putAction()
     {
      $this->getResponse()
           ->appendBody("From putAction() updating the requested article");
     }
 public function deleteAction()
     {
      $this->getResponse()
           ->appendBody("From deleteAction() deleting the requested article");
     } 
}

Above is my REST API I am trying to call it from php curl but I don't know how to call post method. I have also made entry in bootsrap to default module\ using rest route.

Here is a snippet of my code:

<?php
    $ch = curl_init('http://apanel3.newfront.local/api');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT     5.1)");
    $curlresult = curl_exec($curl_connection);
    print_r($curlresult);
?>

I am trying to call my api using following curl code. It is calling indexAction. Even thought i have set curlopt_post to true, I am not getting desired output.

ro ko
  • 2,596
  • 2
  • 31
  • 51
Rohit Jain
  • 29
  • 5
  • some body please help me out i am new to zend. its been jus 2-3 months i am workin on to it. i have a deadline coming soon – Rohit Jain Mar 15 '13 at 11:36

1 Answers1

0

I believe there are lot of examples for php curl + post. Do you know how to access your actions (make http calls generally, without curl) ?

Here is another answer to the link to the similar question.

If you are trying to access your API from another zend based application and want to use zend inbuilt method then, you should check Zend_Http_Client there is an adapter for curl if you want to specifically use curl adapter.

EDIT:

On your client side:

<?php

//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "'http://apanel3.newfront.local/api'");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "query='where post_parameter = query'");

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

curl_close($ch);

// further processing ....
if($server_output == 'OK') {
    echo 'Test passed';
} else {
    echo $server_output;
}
?>

On your API side for indexAction:

public function indexAction()
    {
        $query=$this->getRequest()->getParam('query');
        if($this->getRequest()->isPost()) {

          // method == post, do your processing whatever required here ....

            $this->_forward('post',null,null,$query);
        } elseif ($this->getRequest()->getMethod() == 'GET') {
            // method == get
            $this->_forward('get',null,null,$query);
        }else {               
           // bad request response code 400

            $this->getResponse()
                ->appendBody("not a valid request");
            $this->getResponse()->setHttpResponseCode(400);
        }

    }

Edit:

My mistake I didn't realize you were extending Zend_Rest_Controller, your code for controller seems fine. Now you probably know about making curl request via PHP.

On how to make PUT Request please check this question

Community
  • 1
  • 1
ro ko
  • 2,596
  • 2
  • 31
  • 51
  • actually what i want to know is how client will be calling callin this api. i mean api for put ,api for post,api for get , api for delete. – Rohit Jain Mar 16 '13 at 04:52
  • @RohitJain so are you programming the client that will call this api as well? If yes then I think you can get it done from the above answer. But if you are looking to create some short of documentation or a give away link. Then it will be http://yourhost.com/api/get/id/4. I am really not sure what you are looking for. – ro ko Mar 16 '13 at 04:58
  • please update your question and add what is not working or where you are having trouble, that would be lot more better; and you can get help faster. As I said earlier you can perhaps use zend_http_client if the client is programmed in zend as well. It is relatively easier. – ro ko Mar 16 '13 at 05:03
  • first of all i am sorry fro unstructured question. first thin i want to know is my restful class proper. if yes how client can can call diff methods such as put get post delete. – Rohit Jain Mar 16 '13 at 05:20
  • According to my interpretation after settion curlopt_post to true, it should automatically call post method. am I right or wrong? please help. . – Rohit Jain Mar 16 '13 at 05:23
  • And what is wrong or what is not working? Firs of all none of your action has any verification for post parameter. I've added a snippet of code to my answer please check the edit portion. This is a tested code and works fine. – ro ko Mar 16 '13 at 11:35
  • i read your edit what i am doin currently is calling index action from client code as you shown and then i check which method is used in curl. for e.g say if post method is set then i am calling post method of my rest controller from index action using $this->_forward('post',null,null,params). Is it right way to implment rest or to respond rest request? i hope you getting my question. – Rohit Jain Mar 16 '13 at 18:48
  • @RohitJain I see what you mean, well you can do it this way as well; its upto you for implementation. You should probably send some dummy variable (POSTFIELDS, 'dummy=5') along with setting (CURLOPT_POST,1) and check for that variable instead (whether it is send via post,get or...) and forward. You should rather define your own standard, instead of having different actions for different methods (post,get) Say, request must be sent via 'forms' with CSRF values or delete request then in such cases I use POST, else I use GET. – ro ko Mar 17 '13 at 01:19
  • 1 more thing for makin rest controller i made some entry in bootstrap. I wrote 1 function _initrestroute() · even if i comment this function rest is wrkin fine. So whats the use of that. That was the only reason i want to knw m i really implementin rest in ryt ways? I refered example on techorus.com while creatin restful api controller. – Rohit Jain Mar 17 '13 at 18:54
  • @RohitJain, great so did it work? How about you ask another question for your another query. If this worked. You should perhaps accept the answer and put another question. This conversation is getting way too long any way. – ro ko Mar 18 '13 at 00:29
  • @ ro ko link to my new ly created question as suggested by you http://stackoverflow.com/questions/15470148/rest-routes-in-zend-rest-controller – Rohit Jain Mar 18 '13 at 05:14
  • @RohitJain ok, was the query specific to this question solved? If yes make a habit of accepting answer, if not wait for better answer. Accepting answer will help you get helps for your questions in the future. Welcome to SO! http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – ro ko Mar 18 '13 at 05:18
  • @RohitJain my apologies, your controller code was just fine I didn't realize you were extending Zend_Rest_Controller. You should test again, perhaps you might feel that your next question is invalid somehow then. Do the test first with your old code for controller/indexAction. – ro ko Mar 18 '13 at 05:32
  • as you are saying my comments are extending conversation here. .is there any way we can chat? – Rohit Jain Mar 18 '13 at 05:35
  • if was ryt then can you please telme how my client side will look like if i am making put request? – Rohit Jain Mar 18 '13 at 05:40
  • @RohitJain You do not have enough reputation to chat. For put request I have edited my answer, I suggest you to do some research your self as well. Nobody is here to do your research for you or solve every problem you get!! no offense ;) – ro ko Mar 18 '13 at 05:52