how to call restful zend controller usin curl in php?

◇◆丶佛笑我妖孽 提交于 2019-12-24 22:07:50

问题


<?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.


回答1:


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



来源:https://stackoverflow.com/questions/15431339/how-to-call-restful-zend-controller-usin-curl-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!