php 使用 CURL 向 Web 服务器发送 XML 发布请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15679090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:43:38  来源:igfitidea点击:

Send an XML post request to a web server with CURL

phpcurlwms

提问by user1919

I am trying to send a request to a web server using php and curl. I haven't done something like this before and although there are many nice examples online I have some difficulties understanding some of the curl commands.

我正在尝试使用 php 和 curl 向 Web 服务器发送请求。我以前没有做过这样的事情,虽然网上有很多很好的例子,但我在理解一些 curl 命令时遇到了一些困难。

This is what I want to do: There is an established web service (for example: Web map service) and I want my php code to send a post XML request to this service. As a respond I want to get an XML file.

这就是我想要做的:有一个已建立的 Web 服务(例如:Web 地图服务)并且我希望我的 php 代码向该服务发送一个 post XML 请求。作为回应,我想获得一个 XML 文件。

This is what I have till now:

这是我到目前为止所拥有的:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, ''); 
    /*curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));*/
    /* curl_setopt($ch, CURLOPT_HEADER, 0);*/
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    /*curl_setopt($ch, CURLOPT_REFERER, '');*/
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ch_result = curl_exec($ch);
    curl_close($ch);
    echo $ch_result;

As I said I am quite new in php and also in using curl and I think I am missing some concepts. My questions are: 1) What is the string (link) that I have to put in the:

正如我所说,我对 php 和使用 curl 还很陌生,我想我错过了一些概念。我的问题是:1)我必须输入的字符串(链接)是什么:

          curl_setopt($ch, CURLOPT_URL, ''); 

Is it the host name of the service which I want to send the request?

它是我要发送请求的服务的主机名吗?

2) In row 6 the variable $xml contains the xml file that I want to send as a request. Is it correct or this variable is supposed to contain something else?

2) 在第 6 行中,变量 $xml 包含我想作为请求发送的 xml 文件。它是正确的还是这个变量应该包含其他东西?

3) In which cases do I need to use a httpheader or header (row3 and row4);

3)在哪些情况下我需要使用httpheader或header(row3和row4);

Thanks for the help. Dimitris

谢谢您的帮助。迪米特里斯

回答by PKeidel

Try it this way:

试试这个方法:

  $url = 'https://android.googleapis.com/gcm/send';
  $ch = curl_init();
  curl_setopt( $ch, CURLOPT_URL, $url );
  curl_setopt( $ch, CURLOPT_POST, true );
  curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch, CURLOPT_POSTFIELDS, "<xml>here</xml>" );
  $result = curl_exec($ch);
  curl_close($ch);

For more details visit: http://php.net/manual/en/function.curl-setopt.php

更多详情请访问:http: //php.net/manual/en/function.curl-setopt.php

回答by til_b

I think using the HTTP classes may be better suited for making HTTP requests.

我认为使用 HTTP 类可能更适合发出 HTTP 请求。

See http://www.php.net/manual/intro.http.php.

请参阅http://www.php.net/manual/intro.http.php

Also, there are specific WMS libraries for PHP, e.g. http://docs.huihoo.com/geoserver/1.6.0/Parsing%20and%20using%20WMS%20capabilities%20with%20PHP.html.

此外,还有用于 PHP 的特定 WMS 库,例如http://docs.huihoo.com/geoserver/1.6.0/Parsing%20and%20using%20WMS%20capabilities%20with%20PHP.html