XML Web Services with HTML deployment

1
$url = 'http://www.webservicex.net//country.asmx/GetCountries?';
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, '');
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                    $result = curl_exec($ch);
                    $err = curl_error($ch);
                    curl_close($ch);

                    echo '<br><br>'.$result.'<br><br>';

            $xml = simplexml_load_string($result);
            $json = json_encode($xml);
            $array = json_decode($json,TRUE);

            print_r($array);

In the HTML File, how should the Select go?

Country List:

<select id="campoPais" name="campoPais">

</select>

to call the countries of the WSDL.

    
asked by danbf 22.03.2017 в 07:01
source

3 answers

0

As well says @Gabriel Díaz-Chirón Muñoz is the best way, I tried to parse this URL and I have not been able.

I offer you a solution that is somewhat "homemade" but that can help you to get out of the way.

What you receive via Curl is a STRING with XML format, if you do not manage to transform it into an object you can clean the chain and exploit it.

    $url = 'http://www.webservicex.net//country.asmx/GetCountries?';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, '');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $result = curl_exec($ch);
    $err = curl_error($ch);
    curl_close($ch);

    $result = substr($result, 114);
    $result = substr($result,0,- 30);
    $result = chop($result);
    $borra  = array("Table","Name","&lt;","&gt;","/");
    $output  = str_replace($borra, '', $result);
    $output = explode(PHP_EOL, $output);

    $i= 0;
    $pais = array();
    foreach ($output as $value) {
        if(strlen($value)>3){
            $pais[++$i] = $value;
        }
    }
    echo '<select id="campoPais" name="campoPais">';

    foreach ($pais as $key => $value) {
        echo '<option value="'.$key.'">'.$value.'</option>';
    }
    echo '</select>';

I hope it serves you. Greetings

    
answered by 22.03.2017 / 16:39
source
0

What I would do is take the xml from the URL you comment and transform it to objects or array and loop and paint the select.

This would be the code:

    $paisesXML = file_get_contents('http://www.webservicex.net//country.asmx/GetCountries?');
    $paisesXML = simplexml_load_string($paisesXML);
    $paises = new SimpleXMLElement($paisesXML);
    echo '<select name="campoPais" id="campoPais">';
        foreach ($paises as $pais) {
            $value = (array) $pais->Name;
            echo '<option value="'.strtolower($value[0]).'">'.$value[0].'</option>';
        }
    echo '</select>';
    
answered by 22.03.2017 в 16:03
0

Short answer:

Replaces 'spain' with the country of the previous select

  $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.webservicex.net/airport.asmx/GetAirportInformationByCountry");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "country=spain");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);
curl_close ($ch);
$paisesXML = simplexml_load_string($server_output);
$paises = new SimpleXMLElement($paisesXML);
    echo '<select name="campoPais" id="campoPais">';
        foreach ($paises as $pais) {
            $value = (array) $pais->CityOrAirportName;
            echo '<option value="'.strtolower($value[0]).'">'.$value[0].'</option>';
        }
    echo '</select>';
    
answered by 27.03.2017 в 20:29