2017-10-07 47 views
0

我想在HTML中显示http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4这个数组数据。JSON数组foreach循环显示提供给foreach的无效参数()

这里是我的代码:

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:

请帮助我。 谢谢

+0

该数组为空。在循环之前检查它 – Akintunde007

+0

代码的图像在问题中是不可接受的。将实际代码放在这里 – apokryfos

+0

您可能想要'json_decode'而不是'json_encode',因为API已经为您提供了一个JSON字符串。摆脱第24行,并用当前行中的$ ob替换'$ json'(这将是新行24)。 – ccKep

回答

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>'; 
}?>