So here is the question. I’m trying to call my api with PHP CURL post like so
<?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);
?>
and In my API which is using zend framework, I received the data like so
<?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';
?>
when I check the value in query, it didn’t received the “page” value passed by using CURL.. Am I doing something wrong?
Cheers
At first, you should check _getParam method that returns POST variables correctly.
Then, Note, you should use json_decode on one really json string! you should decode url data variable(result of $this->_getParam(‘data’,”)) and then pass it to json_decode!
I assume your _getParam method works correctly,
then: