如何在PHP中使用curl调用restent zend控制器?

问题描述:

<?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");
     } 
}

以上是我的 REST API >我试图从php curl调用它,但是我不知道如何调用post方法。
我也使用rest路由在bootsrap中输入了默认模块。

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);
?>

我正在尝试使用以下curl代码调用我的api。它正在调用indexAction。甚至以为我已经将curlopt_post设置为true,我没有得到想要的输出。

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.

我相信有很多php示例卷曲+发布。您知道如何访问您的操作(通常进行http调用,没有卷曲)吗?

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

这里是另一个链接到类似问题的链接

如果您试图从另一个基于zend的应用程序访问您的API并想使用zend内置方法,则应检查 Zend_Http_Client 中有一个用于卷曲的适配器,如果您要专门使用卷曲适配器。

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.

编辑:

在您的客户端上:

<?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;
}
?>

在API端进行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:

我的错误我没有意识到您正在扩展Zend_Rest_Controller,您的控制器代码似乎很好。现在您可能知道有关通过PHP发出curl请求的信息。

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.

有关如何制作 PUT 的请求,请检查此问题

On how to make PUT Request please check this question