如何通过curl将帖子请求发送到php中的web服务

时间:2014-05-28 05:04:18

标签: php web-services api curl

我需要从myallocator.com获取数据。我有他的api地址api.myallocator.com, 但是当我向他们发送数据时,它失败了。我写了一些代码,如下所示。基本上是pms物业管理系统的网站。通过他的api可以管理你的财产。

    $url = "http://api.myallocator.com/";

    $xmlRequestString='<?xml version="1.0" encoding="UTF-8"?>
    <GetProperties>
      <Auth>
        <UserId>"xxxxxxx"</UserId>
        <UserPassword>"xxxxx"</UserPassword>
        <VendorId>"xxxxxxx"</VendorId>
        <VendorPassword>"xxxxxxxx"</VendorPassword>
      </Auth>
    </GetProperties>';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $xmlRequestString);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    $data = curl_exec($ch);
    echo $data;
    curl_close($ch);

1 个答案:

答案 0 :(得分:1)

这是php curl的例子,帖子试试这个

        <?php
        $ch = curl_init();            
        // set your site url
        curl_setopt($ch, CURLOPT_URL,"http://testmysite.com/");

        curl_setopt($ch, CURLOPT_POST, 1);
        // postvar1, postvar2 .. are the parameters to send with post
        curl_setopt($ch, CURLOPT_POSTFIELDS,
                    "postvar1=value1&postvar2=value2&postvar3=value3");          
        // receive server response ...
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $server_output = curl_exec ($ch);            
        curl_close ($ch);

        if ($server_output == "OK") { ... } else { ... }

        ?>