在php中进行Web服务调用

时间:2016-02-10 22:36:01

标签: php ruby web-services httpresponse httpsession

所以我在ruby中工作,我希望能够在php中完成此操作。如果重要,我正在使用wamp服务器。

这是ruby方法:

def response(url, body)
 uri = URI(url)

 request = Net::HTTP::Post.new(uri.request_uri)
 request.body = body

 http_session = Net::HTTP.new(uri.hostname, uri.port)
 http_session.use_ssl = (uri.scheme == "https")
 http_session.verify_mode = OpenSSL::SSL::VERIFY_NONE
 response = http_session.request(request)

 return response.body
end

我试着查找其他问题,这就是我的所在:

$request_info = array();
$REQUEST_BODY = 'request body';

$full_response = @http_post_data(
    'url',
    $REQUEST_BODY,
    array(
        'headers' => array(
            'Content-Type' => 'text/xml; charset=UTF-8',
            'SOAPAction' => 'HotelAvail',
        ),
        'timeout' => 60,

    ),
    $request_info
);

$response_xml = new SimpleXMLElement(strstr($full_response, '<?xml'));

foreach ($response_xml->xpath('//@HotelName') as $HotelName) {
    echo strval($HotelName) . "\n";
}

1 个答案:

答案 0 :(得分:0)

http_post_data取决于pecl_http。除非必须使用http_post_data,否则默认情况下可能会在您的WAMP服务器上安装cURL。

以下代码只是一个例子;我还没有测试过,但你明白了这个想法:

$headers = array(
    'Content-Type' => 'text/xml; charset=UTF-8',
    'SOAPAction' => 'HotelAvail',
);

$ch = curl_init($server_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$full_response = curl_exec($ch);
curl_close($ch);