如何在php中使用curl调用restful zend控制器?

时间:2013-03-15 11:33:12

标签: php zend-framework rest curl call

<?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中输入默认模块\。

  

以下是我的代码片段:

<?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,但我没有获得所需的输出。

1 个答案:

答案 0 :(得分:0)

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

这是类似问题的另一个answer to the link

如果您尝试从另一个基于zend的应用程序访问您的API并希望使用zend inbuilt方法,那么如果您想专门使用curl适配器,则应该检查Zend_Http_Client是否有adapter for 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 请求check this question