JSON数组foreach循环显示为foreach()提供的无效参数

时间:2017-10-07 17:41:12

标签: php arrays json

我想在HTML中显示http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4这个数组数据。

这是我的代码:

ini_set("display_errors", 1);

$url = "http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4";
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);        // set the fields to post 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        // make sure we get the response back 
$xml = curl_exec($ch);      // execute the post 
curl_close($ch);                // close our session 

include "XMLParser.php";
$parser = new XMLParser();
$tour = $parser->parseString($xml);

print_r($tour);

//$errors = array_filter($tour);

if (empty($tour)) {
echo 'Error';
 }else{
  echo 'No Error';
 }

  //print_r($xml);
  //var_dump( $xml);
 //echo $xml;
    //$xmlfile = file_get_contents($tour);
    $ob= simplexml_load_string($tour);
    //$json  = json_encode($ob);
    $configData = json_decode($ob, true);

    foreach($configData as $result){
     echo '<tr>';
        echo '<td>'.$result->name.'</td>';
        echo '<td>'.$result->phone.'</td>';
        echo '<td>'.$result->email.'</td>';
      echo '</tr>';
    }

我收到此错误Warning: Invalid argument supplied for foreach() in C:

请帮帮我。 感谢

1 个答案:

答案 0 :(得分:0)

这是我的最终工作代码,以帮助需要此代码的其他人。

<?php
ini_set("display_errors", 1);
//$days = "http://services.xyz.com/tours.asmx/Days?type=2&cityID=1146";
$url = "http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4";//Replace this with your own Web services Link
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        // make sure we get the response back 
$xml = curl_exec($ch);      // execute the post 
curl_close($ch);                // close our session 
$var = simplexml_load_string($xml);

$configData = json_decode($var, true);

/*echo '<pre>';
    print_r($configData);
    echo '</pre>';*/

foreach ($configData as $row) {
echo 'Day:' . $row['Day'].'<br>';
echo 'Time:' .$row['Time'].'<br>';
echo 'Description:' .$row['Description'].'<br>';
}?>