HTTP POST请求

时间:2018-10-27 09:23:48

标签: xml curl http-post

商家网站和Web服务之间的交互是通过HTTP POST请求执行的。 POST应该包含一个表格字段“ request”。该字段的内容是请求XML。随着 以下结构:

void FillPool()
{
    for(uint32_t i = 0; i < PoolIncrementSize; i++)
    {
        if(measurePool[i] == nullptr)
            measurePool[i] = new measure();
    }
    poolCursorPosition = PoolIncrementSize;
}

measure_ptr GetElementFromPool()
{
    if(poolCursorPosition == 0)
    {
        FillPool();
    }
    poolCursorPosition--;
    measure_ptr element = measurePool[poolCursorPosition];
    measurePool[poolCursorPosition] = nullptr;
    return element;
}

void ReturnElementToPool(measure_ptr element)
{
    if(poolCursorPosition == poolSize)
        ResizePool();

    measurePool[poolCursorPosition] = element;

    poolCursorPosition++;
}

响应样本:

   <request 
        action="getCategories" 
        passcode="123456" 
      language="en">
    </request>

我尝试过CURL(已编辑并可以使用):

<response errorcode="0" errormessage="">
  <categories> 
    <category> 
      <id>46</id>
      <title><![CDATA[Accesories]]></title>
    </category>
    <category>
      <id>122</id>
      <title><![CDATA[Media accesories]]></title>
    </category>
   </categories>
</response>

一切都很好... 我收到:

 $xml_data = ['request' => '<request 
    action="getCategories" 
    passcode="123456" 
  language="en">
</request> '];    



   $headers = array(
            "Content-type: text/xml"
           // "Content-length: " . strlen($xml_data)
           // "Connection: close"
        );

        $url = "http://remote.server.com/api";
                    $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
       // curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
       // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
       // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
       // curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($xml_data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $output = curl_exec($ch);

        curl_close($ch);



        print_r(($xmlData));

(此示例中的服务器名称已更改)

所以我的代码现在可以工作了... 谢谢大家的帮助。 :P

0 个答案:

没有答案