7

ここで、Google ドライブ スクリプトを使用して Web サービスを呼び出す方法の例を見つけました: https://developers.google.com/apps-script/articles/soap_geoip_example

function determineCountryFromIP(ipAddress) {
    var wsdl = SoapService.wsdl("http://www.webservicex.net/geoipservice.asmx?wsdl");
    var geoService = wsdl.getGeoIPService();

    var param = Xml.element("GetGeoIP", [
              Xml.attribute("xmlns", "http://www.webservicex.net/"),
              Xml.element("IPAddress", [
                ipAddress
              ])
            ]);

    var result = geoService.GetGeoIP(param);
    return result.Envelope.Body.GetGeoIPResponse.GetGeoIPResult.CountryCode.Text;
}

ただし、これは非推奨の SoapService を使用します。ドキュメントには、UrlFetchAppを使用する必要があると記載されて います。入力 xml の変換は簡単です。しかし、UrlFetchApp を使用して Web サービスを呼び出す方法を誰か教えてもらえますか?

4

1 に答える 1

10

それはもっと多くの作業であることが判明しましたが、1日グーグルで試してみた後、UrlFetchAppで動作するようになりました

function UrlFetchAppDetermineCountryFromIP_(ipAddress) {
  var xml =          
     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    +"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
      +"<SOAP-ENV:Body>"
        +"<GetGeoIP xmlns=\"http://www.webservicex.net/\">"
          +"<IPAddress>"+ ipAddress +"</IPAddress>"
        +"</GetGeoIP>"
      +"</SOAP-ENV:Body>"
    +"</SOAP-ENV:Envelope>"

  var options =
  {
    "method" : "post",
    "contentType" : "text/xml",
    "payload" : xml
  };

  var result = UrlFetchApp.fetch("http://www.webservicex.net/geoipservice.asmx?wsdl", options);

  var xmlResult = XmlService.parse(result).getRootElement();
  var soapNamespace = xmlResult.getNamespace("soap");
  var getGeoIPResponse = xmlResult.getChild("Body", soapNamespace).getChildren()[0];
  var getGeoIPResponseNamespace = getGeoIPResponse.getNamespace();

  return getGeoIPResponse
    .getChild("GetGeoIPResult", getGeoIPResponseNamespace)
    .getChild("CountryCode", getGeoIPResponseNamespace)
    .getText();
}

XmlService を使用してペイロード xml を構築することはおそらく可能なはずですが、数時間試してみたところ、 Evnelope要素に 4 つの xmlns 属性を配置できなかったため、Web サービス リクエストが失敗しました。

于 2013-08-10T14:43:02.783 に答える