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

以上是我试图从 php curl 调用它的REST API,但我不知道如何调用 post 方法。我还使用休息路线在引导程序中进入默认模块\。

这是我的代码片段:

<?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,我也没有得到想要的输出。

4

1 回答 1

0

我相信 php curl + post 有很多例子。您知道如何访问您的操作(通常进行 http 调用,不使用 curl)吗?

这是类似问题的链接的另一个答案。

如果您尝试从另一个基于 zend 的应用程序访问您的 API 并想使用 zend 内置方法,那么您应该检查Zend_Http_Client如果您想专门使用 curl 适配器是否有一个curl 适配器。

EDIT:

在您的客户端:

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

在 indexAction 的 API 端:

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 请求了。

关于如何提出PUT请求,请检查这个问题

于 2013-03-15T14:31:31.133 回答