php cURL - post和echo result:“xml结构错误或编码解码错误...”

时间:2016-12-09 20:21:59

标签: php curl

我尝试通过cURL发布并获得结果,但还没有成功......

描述 - 信息手册说:“要向服务器发送请求,你必须发送带有在base64中编码的变量xml的POST请求.Xml变量由XML特殊格式组成。检查PHP:

   $curl = curl_init();
   curl_setopt($curl, CURLOPT_URL, 'http://test.client-shop-logistics.ru/index.php?route=deliveries/api');
   curl_setopt($curl, CURLOPT_POST, 1);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_POSTFIELDS, 'xml='.urlencode(base64_encode($xml)));
   curl_setopt($curl, CURLOPT_USERAGENT, 'Opera 10.00');
   $res = curl_exec($curl);
   curl_close($curl);

请求示例:

<request>
  <function>get_order_status_array</function>
  <api_id>API_ID</api_id>
  <deliveries>
    <code>135834675500056751</code>
    <code>135834675500056750</code>
    <code>135833991800056758</code>
  </deliveries>
</request>

答案示例:

<answer>
  <error>0</error>
  <deliveries>
    <delivery>
      <code>135834675500056750</code>
      <order_id>400004</order_id>
      <status>New</status>
      <payment_status/> 
      <vozvrat_status/> 
      <errors/>
      <pickup_id/>
      <oplacheno_poluchatelem>0</oplacheno_poluchatelem>
      <products>
        <product>
          <articul>4556-56</articul>
          <name>test name</name>
          <quantity>10</quantity>
          <price>1000</price>
          <number_of_return>0</number_of_return>
          <number_of_delivered>0</number_of_delivered>
          <status/>
        </product>
        <product>
          <articul>22229</articul>
          <name>Test product</name>
          <quantity>20</quantity>
          <price>2000</price>
          <number_of_return>0</number_of_return>
          <number_of_delivered>0</number_of_delivered>
          <status/>
        </product>
      </products>
    </delivery>
    <delivery>
      <code>135834675500056751</code>
      <order_id>400003</order_id>
      <status>New</status>
      <payment_status/>
      <vozvrat_status/> 
      <errors/>
      <pickup_id/>
      <oplacheno_poluchatelem>0</oplacheno_poluchatelem> 
      <products>
        <product>
          <articul>22229-22</articul>
          <name>test name</name>
          <quantity>40</quantity>
          <price>4000</price>
          <number_of_return>0</number_of_return>
          <number_of_delivered>0</number_of_delivered>
          <status/>
        </product>
        <product>
          <articul>4556-44</articul>
          <name>test name</name>
          <quantity>30</quantity>
          <price>3000</price>
          <number_of_return>0</number_of_return>
          <number_of_delivered>0</number_of_delivered>
          <status/>
        </product>
      </products>
    </delivery>
    <delivery>
      <code>135833991800056758</code>
      <order_id>600003</order_id>
      <status>Completed</status>
      <payment_status/>
      <errors></errors>
      <pickup_id/>
      <oplacheno_poluchatelem>0</oplacheno_poluchatelem>
    </delivery>
  </deliveries>
</answer>

这是我的测试php文件(我尝试从我的网站运行的testcurl.php),使用测试api_id / url:

<?php

// build xml        
$xml = '
<?xml version="1.0" encoding="UTF-8"?>
<file>
<request>
  <function>get_order_status_array</function>
  <api_id>577888574a3e4df01867cd5ccc9f18a5</api_id>
  <deliveries>
    <code>135834675500056751</code>
    <code>135834675500056750</code>
    <code>135833991800056758</code>
  </deliveries>
</request>
</file>';

       $curl = curl_init();
       curl_setopt($curl, CURLOPT_URL, 'https://test.client-shop-logistics.ru/index.php?route=deliveries/api');
       curl_setopt($curl, CURLOPT_POST, 1);
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($curl, CURLOPT_POSTFIELDS, 'xml='.urlencode(base64_encode($xml)));
       curl_setopt($curl, CURLOPT_USERAGENT, 'Opera 10.00');
       $res = curl_exec($curl);
       curl_close($curl);

       echo $res;

?>

最后我收到一个错误:“结构xml错误或编码错误编码:get_order_status_array 577888574a3e4df01867cd5ccc9f18a5 135834675500056751 135834675500056750 135833991800056758”

我缺少什么?

0 个答案:

没有答案