如何在php中使用curl调用restful zend控制器?
我相信php curl+post有很多例子。您知道如何访问您的操作(通常进行http调用,而不使用curl) 这是另一个类似问题的答案 如果您试图从另一个基于zend的应用程序访问API,并且希望使用zend内置方法,那么您应该检查是否存在一个专门使用curl适配器的方法如何在php中使用curl调用restful zend控制器?,php,zend-framework,rest,curl,call,Php,Zend Framework,Rest,Curl,Call,我相信php curl+post有很多例子。您知道如何访问您的操作(通常进行http调用,而不使用curl) 这是另一个类似问题的答案 如果您试图从另一个基于zend的应用程序访问API,并且希望使用zend内置方法,那么您应该检查是否存在一个专门使用curl适配器的方法 编辑: 在您的客户端: 我的错误我没意识到你在扩展Zend_Rest_控制器,你的控制器代码看起来很好。现在您可能知道如何通过PHP发出curl请求 关于如何提出请求,请一些人帮助我,我是新来的zend。已经2-3个月了,我
编辑:
在您的客户端:
我的错误我没意识到你在扩展Zend_Rest_控制器,你的控制器代码看起来很好。现在您可能知道如何通过PHP发出curl请求
关于如何提出请求,请一些人帮助我,我是新来的zend。已经2-3个月了,我正在努力。我的最后期限很快就要到了,实际上我想知道的是客户端将如何调用这个api。我指的是用于put的api、用于post的api、用于get的api、用于delete的api。@RohitJain那么,您是否也在为调用此api的客户端编程?如果是的话,我想你可以通过上面的答案来完成。但是,如果您希望创建一些简短的文档或赠送链接。那就是了。我真的不知道你在寻找什么。请更新你的问题,并补充什么是不工作或你有麻烦,这将是更好的;你可以更快地得到帮助。正如我前面所说的,如果客户端也是用zend编程的,那么您也许可以使用zend_http_客户端。这相对比较容易。首先,对于这个没有条理的问题,我很抱歉。我想知道的第一个问题是我的restful类是否合适。如果是,客户端如何调用diff方法,如put-get-post-delete。根据我的解释,将curlopt_-post设置为true后,它应该自动调用post方法。我是对还是错?请帮忙。
<?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
$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);
?>
<?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;
}
?>
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: