PHP foreach在API调用中显示国家名称
我使用经销商面板API解决以下问题 我有一个带有foreach的Curl脚本。这是从分销商面板api获取国家/地区列表PHP foreach在API调用中显示国家名称,php,curl,foreach,Php,Curl,Foreach,我使用经销商面板API解决以下问题 我有一个带有foreach的Curl脚本。这是从分销商面板api获取国家/地区列表 function getCountries() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "myapiurl"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, POST); cur
function getCountries() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "myapiurl");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$final = curl_exec($ch);
$result = explode(" ", $final);
echo '<select>';
foreach ($result as $country => $name) {
echo '<option>'.$country. '</option>';
}
echo '</select>';
curl_close($ch);
}
我希望能够使用foreach语句在select中呼出国家名称
例:
我认为你把绳子炸错了。你是爆炸,空字符串。但基于您的返回,我假设它是XML节点格式的。如果您打印出所有内容,它将显示所有节点信息,但您只需要输入。尝试用户simplexml_加载_字符串,是一个php函数。例如。 所以您可以直接访问名称节点。希望这有帮助
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "myapiurl");
curl_setopt($ch, CURLOPT_POST, 1);
# curl_setopt($ch, CURLOPT_POSTFIELDS, POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$final = curl_exec($ch);
$domd = new DOMDocument();
$domd->loadXML($final);
$countries = $domd->getElementsByTagName('countries')->item(0)->childNodes;
echo '<select>';
foreach($countries AS $country){
// the following is just to make sure we only get tags with the name "n\d+"
if(preg_match('/n\d+/', $country->tagName)){
$name = $country->getElementsByTagName('name');
// to make sure there's a sub-tag named "name"
if($name->length!=1) continue;
echo '<option value="'.$country->tagName.'">'
.$name->item(0)->nodeValue.'</option>';
}
}
echo '</select>';
我注释掉的curl_setopt行毫无意义,应该给您一个警告或错误。POST应该是一个带有POST值的长字符串,类似于url中的get值。variable1=value1&variable2=value2&variable3=value3等。这用于向服务器发送特定的调用,因此可能会给您不同的输出,例如仅几个国家而不是所有国家
您也可以按照Steven的建议使用simplexml。这几乎是一样的。这不重要。循环foreach$结果为$country=>$name{定义$country是键,$name是值,这就是为什么你得到的是索引而不是国家名。试试echo。$name。@TrungDQ这应该是答案。如果你能解释为什么会这样,并加入到mdn的链接那就太好了。Mitch Evans,你能给我们一小段你在echo$final时得到的文本吗您希望如何从中提取数据?这只是一个名称列表,那么TrungDQ所说的是正确的;foreach$array AS$key=>$value。当您创建数组时,您会自动从0..n中分配密钥。如果它是一个XML文件,那么您可能想用它来处理它。@HorseSMith-检查更新的问题。@Mitch Evans编写一个函数在此基础上,将字符串拆分为您想要的数组。如果您了解regex,您可能可以非常轻松地完成此操作。这可能比答案二更通用……今晚晚些时候我将检查此问题,并可能接受此问题作为答案。根据您的API输出,它显示它是XML格式的。每个问题都会被标记,并且您需要做的是将它们放入数组中,按节点名称编制索引。如果可能,请尝试更新代码。这可用于获取国家名称并将其格式化为一个很好的选择选项。但是,它不适用于获取我的计划名称、客户信息,甚至获取我的数据中心。我需要更通用的东西所有我需要改变的是一些事情来让它工作。我已经厌倦了与之抗争,我想要一个简单的解决方案。试着理解代码并做任何你需要的改变。如果你想要iso2标签的值,那么就和我对名称标签做的一样。
<ampa>
<command>"0"
<error_code>0</error_code>
<countries>
<n0>
<name>Afghanistan</name>
<iso2>AF</iso2>
<phone_code>93</phone_code>
</n0>
</countries>
</ampa>
<option value="0">
0
0
56000
Afghanistan
AF
93
Albania
AL
355
Algeria
DZ
213
American</option>
0 0 56000 Afghanistan AF 93 Albania AL 355 Algeria DZ 213 American Samoa AS 684 Andorra AD 376 Angola AO 244 Anguilla AI 1 Antarctica AQ 672 AG 1 Argentina AR 54 Armenia AM 374 Aruba AW 297 Australia AU 61 Austria AT 1 0 0.2 43
<select>
<option>United States</option>
</select>
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "myapiurl");
curl_setopt($ch, CURLOPT_POST, 1);
# curl_setopt($ch, CURLOPT_POSTFIELDS, POST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$final = curl_exec($ch);
$domd = new DOMDocument();
$domd->loadXML($final);
$countries = $domd->getElementsByTagName('countries')->item(0)->childNodes;
echo '<select>';
foreach($countries AS $country){
// the following is just to make sure we only get tags with the name "n\d+"
if(preg_match('/n\d+/', $country->tagName)){
$name = $country->getElementsByTagName('name');
// to make sure there's a sub-tag named "name"
if($name->length!=1) continue;
echo '<option value="'.$country->tagName.'">'
.$name->item(0)->nodeValue.'</option>';
}
}
echo '</select>';