1

私は今日何時間もコードを扱ってきましたが、誰かが私を今よりも良い方向に向けることができるかどうか疑問に思っています。

send_request_via_curl($ host、$ path、$ content)を介してXMLを送信することにより、データの配列をフェッチするPHPコードがあります。

私の機能:

function send_request_via_curl($host,$path,$content)
{
$posturl = "https://" . $host . $path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml;charset=utf-8"));
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    return $response;
}

$headers = array(
    "Content-type: text/xml;charset=utf-8",
    "Accept: application/xml",
    "Cache-Control: no-cache",
    "Pragma: no-cache",
    "Content-length: " . strlen($content),
    );


$content = '<?xml version="1.0" encoding="utf-8"?>
                <soap:Envelope    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                <soap:Body>
                <GetLocations xmlns="http://tempuri.org/">
                    <AuthLogin>ClientName</AuthLogin>
                    <UserName>MyUser</UserName>
                    <Password>SomePassword</Password>
                </GetLocations>
            </soap:Body>
            </soap:Envelope>';

$response = send_request_via_curl($host,$path,$content);

編集:

近づいてきて、Babaのコードを実装したところ、別のエラーが発生しました。

警告:不明:ノードはもう存在しません...。

上記と同じコードがありますが、これで応答が処理されます。

if ($response)
{

$xml = new SimpleXMLElement($response);
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$path = $xml->xpath("//soap:Body");
$path = $path[0] ;
foreach($path->GetLocationsResponse->GetLocationsResult->SearchLocations->children() as $table)
{
echo $table->Table->LocationName . "<br>";
}

返される真のXMLは次のとおりです。FirebugがすべてのXMLを小文字にしたのに対し、ソースコードには大文字が混在していることに気づきました。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetLocationsResponse xmlns="http://tempuri.org/">
<GetLocationsResult>
<SearchLocations>
<Table>
<LocationName>
</Table>
<Table>
<LocationId>103501</LocationId>
<LocationName>Albuquerque, New Mexico, USA</LocationName>
</Table>
<Table>
<LocationId>101600</LocationId>
<LocationName>Atlanta, Georgia, USA</LocationName>
</Table>
</SearchLocations>
</GetLocationsResponse>
</GetLocationsResult>
</soap:Body>
</soap:Envelope>

ありがとう!!!

4

2 に答える 2

3

あなたが得た応答はそうではありませんHTMLXML、あなたの XML が間違っているようです 無効なタグがあるか、間違いを犯したに違いありません ... を参照してください

  <LocationName>Hawaii</Location>
                             ^--- it should be LocationName

このようになるはずです

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <getlocationsresponse xmlns="http://tempuri.org">
            <getlocationsresult>
                <SearchLocations>
                    <Table>
                        <LocationID>10322</LocationID>
                        <LocationName>Hawaii</LocationName>
                    </Table>
                </SearchLocations>
            </getlocationsresult>
        </getlocationsresponse>
    </soap:Body>
</soap:Envelope>

この XML を読む

$xml = new SimpleXMLElement($data);
$xml->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
$path = $xml->xpath("//soap:Body");
$path = $path[0] ;
foreach($path->getlocationsresponse->getlocationsresult->SearchLocations->children() as $table)
{
     echo $table->LocationName . "<br>";
}

出力

 Hawaii

- - - 編集 - - - -

あなたの新しい XML はまた間違っています..これはhttp://codepad.org/JgJfqnrAのようになるはずです

于 2012-10-13T01:52:22.300 に答える
1

Baba は正しいです。XML を解析するための組み込み関数がこれを行うための最良の方法ですが、XML が実際に不一致のタグで送り返された場合は、正規表現を使用することもできます。

function regex_all( $capture, $haystack, $return=1 ) {
  preg_match_all( "#$capture#", $haystack, $match );
  return $match[ $return ];
}

foreach( regex_all('<LocationName>(.*?)<\/', $data) as $locationName ) {
  echo "$locationName<br>";
}

正規表現は信頼性が低いため、これは推奨される方法ではありません。

于 2012-10-13T02:43:59.730 に答える