简体   繁体   English

PHP CURL POST字段

[英]PHP CURL POST fields

So here is the question. 所以这是问题。 I'm trying to call my api with PHP CURL post like so 我试图像这样用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);
?>

and In my API which is using zend framework, I received the data like so 在使用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';
?>

when I check the value in query, it didn't received the "page" value passed by using CURL.. Am I doing something wrong? 当我检查查询中的值时,它没有收到使用CURL传递的“页面”值。我在做错什么吗?

Cheers 干杯

At first, you should check _getParam method that returns POST variables correctly. 首先,您应该检查_getParam方法,该方法正确返回POST变量。

Then, Note, you should use json_decode on one really json string! 然后,请注意,您应该在一个真正的json字符串上使用json_decode! you should decode url data variable(result of $this->_getParam('data','')) and then pass it to json_decode! 您应该解码url数据变量($ this-> _ getParam('data','')的结果),然后将其传递给json_decode!

I assume your _getParam method works correctly, then: 我假设您的_getParam方法可以正常工作,然后:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM