PHP CURL POST字段

时间:2012-09-24 05:03:49

标签: php zend-framework

所以这就是问题所在。我试图用PHP CURL帖子调用我的api,如此

<?php
        $ch = curl_init();
        $data = array('page' => $pageid);
        $data = json_encode($data);

        $fields = 'data=' . urlencode($data);

        /*echo $fields; ## data=%7B%22page%22%3A%222%22%7D */

        curl_setopt($ch, CURLOPT_URL, 'http://www.myurl.com/api/gallery');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $result =curl_exec($ch);

        $products=json_decode($result,true);
?>

并且在我使用zend框架的API中,我收到了类似的数据

<?php
        $data = json_decode($this->_getParam('data',''), true);
        $query = array();
        $query['status'] = '1';
        $query['pid']   = (isset($data['pid']))? $data['pid'] : '';
        $query['search'] = (isset($data['search']))? $data['search'] : '';
        $query['orderby'] = (isset($data['sort']))? $data['sort'] : 'latest';
        $query['page'] = (isset($data['page']))? $data['page'] : '1';
        $query['pagelimit'] = (isset($data['pagelimit']))? $data['pagelimit'] : '2';
?>

当我检查查询中的值时,它没有收到使用CURL传递的“页面”值。我做错了什么?

干杯

1 个答案:

答案 0 :(得分:0)

首先,您应该检查正确返回POST变量的_getParam方法。

然后,注意,你应该在一个真正的json字符串上使用json_decode!你应该解码url数据变量($ this-&gt; _getParam('data','')的结果),然后将它传递给json_decode!

我假设您的_getParam方法正常工作, 然后:

$data = json_decode(urldecode($this->_getParam('data','')), true);
$query = array();
.
.
.