douxun4173 2015-04-02 10:24 采纳率: 0%
浏览 225
已采纳

空数组输出结果

I hope you can help identify where I am making mistakes in the following code:

    public function yee() {

    $json = array();
    $params = array(
                'token'             => $this->_token,
                'receiver_name'         => 'Test Name',

                'sender_city'           => 'copenhagen',
                'sender_country'        => 'DK',
                'order_id'              => 150
            );

    $ch = curl_init();      
    $query = http_build_query($params);

        curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT . '/' . 'shipments/imported_shipment');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            $output = curl_exec ($ch);
            $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            curl_close ($ch);

            $output = json_decode($output, true);

            if ($http_code != 200){
                if(is_array($output['message'])){
                        $json['error'] = $output['message'];
                }else{
                        $json['error'] = $output['message'];
                }
            } else {
                $json['error'] = "Import Successully";
            } 
    return $output;
        }
    }

I have this in my class.php file, along with a few more lines doing the authentication.

Then I try to call the function after initializing it:

$testObject->yee();
echo $testObject->yee(); ?>

The authentication part works correctly, as I'm also outputting tokens and a few other variables, however when I echo the yee() function - all it displays is "Array"

In case you didn't notice, I am extremely green in JSON/API territory, and my PHP skills are rusty at best. So I am hoping for any and all pointers to lead me closer to solving this. Thanks!

  • 写回答

2条回答 默认 最新

  • dte8665 2015-04-02 10:50
    关注

    I think you already get the idea from steve's comment. You just can't echo an array. Please use,

    var_dump($output);
    //or
    print_r($output);
    

    to debug.

    Or if you just want to stick on echo, try:

    echo implode( ', ', $output);
    

    Thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向