duanmao2650 2012-04-12 21:18
浏览 11

从PHP中删除CURL中的“Array()”结果

I'm trying to fetch data from a password protected JSON feed using curl, but the result adds "Array (" to the beginning of the feed and ")" at the end, making it invalid.

I'm using this code:

<?php
  $url = 'https://slx.arlcap.com/sdata/rcs/tablet/products/Y6UJ9A00000Z/filings';
  $username = 'xxx';
  $password = 'xxx';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
  curl_setopt($ch, CURLOPT_HEADER, false);
  //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  //curl_setopt($ch, CURLOPT_REFERER, $url);
  $result = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);
  $data = json_decode($result, true);
  print_r($data);
?>

And the result is here: http://www.motion.tc//DataTables-1.9.0/examples/ajax/feed.php

Is there a way I can have the data return without the "Array()" element being added?

Thanks! Andrew

  • 写回答

2条回答 默认 最新

  • duanmu8911 2012-04-12 21:23
    关注

    That is because you're using a print_r function. To get only the values inside of the variable, do an echo with a foreach loop. Something like this:

    foreach( $data as $key => $val ) {
      echo $key . " -> " . $val;
    }
    

    If it isn't a nested array. If it is nested array, search SO for printing them effectively.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?