2017-10-07 15 views
0

http://services.xyz.com/tours.asmx/Sample?type=2&CityID=1146&days=4この配列データをHTMLに表示したいとします。私はこのエラーを取得しています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_encode'ではなく' json_decode'が必要です。そのAPIはすでにJSON文字列を与えているからです。 24行目を取り除き、現在の25行目に '$ json'を' $ ob'に置き換えます(新しい行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>'; 
}?> 
関連する問題