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条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)