简体   繁体   English

使用CURL将XML发布请求发送到Web服务器

[英]Send an XML post request to a web server with CURL

I am trying to send a request to a web server using php and curl. 我正在尝试使用php和curl向Web服务器发送请求。 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. 我之前没有做过类似的事情,虽然网上有很多不错的例子,但我对理解一些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. 这就是我想要做的:有一个已建立的Web服务(例如:Web地图服务),我希望我的php代码向此服务发送一个发布XML请求。 As a respond I want to get an XML file. 作为回应,我想获得一个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. 正如我所说,我在php中也很新,也使用curl,我想我缺少一些概念。 My questions are: 1) What is the string (link) that I have to put in the: 我的问题是: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. 2)在第6行中,变量$ xml包含我要作为请求发送的xml文件。 Is it correct or this variable is supposed to contain something else? 它是正确的还是这个变量应该包含其他内容?

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 季米特里斯

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

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, eg 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM