如何将此PUT请求发送到prestashop?

时间:2015-10-18 20:05:21

标签: php xml rest f# prestashop

我正在使用Prestashop网络服务。我正在尝试向API发送PUT(更新)请求,但没有运气。我的请求似乎是以'错误'的方式设置的(即不是服务器期望的方式)因为Prestashop是开源的,所以我看了一下源代码,特别是当它收到PUT请求时执行以下操作(我不编写php代码):

$input_xml = null;
// if a XML is in PUT or in POST
if (($_SERVER['REQUEST_METHOD'] == 'PUT') || ($_SERVER['REQUEST_METHOD'] == 'POST')) {
    $putresource = fopen("php://input", "r");
    while ($putData = fread($putresource, 1024)) {
        $input_xml .= $putData;
    }
fclose($putresource);
  }
if (isset($input_xml) && strncmp($input_xml, 'xml=', 4) == 0) {
    $input_xml = substr($input_xml, 4);
}

从上面的代码中我了解到我的数据看起来应该是这样的:xml=<data><here></here></data>但我不知道在哪里放这个,它应该在请求体中还是嵌入在url中?当您使用Content-Type = text/xml发送请求时隐含“xml =”?我确实尝试了不同的请求组合,但仍然遇到相同的404错误。我试过这个:

let updateOrderState (orderId:int64) (stateId:int64) (credentials:AuthInfo) = 
    // url looks like this: http://www.webstoreexample.com/entity/id
    let auth = BasicAuth credentials.Key ""
    let orderApi = credentials.Api + "/orders/" + orderId.ToString();
    let orderAsXml = Http.RequestString(orderApi, httpMethod = "GET", headers = [auth])
    let xml = Order.Parse(orderAsXml).XElement // at this point, I have the data
    xml.Element(XName.Get("order")).Element(XName.Get("current_state")).SetValue(stateId) // field 'current_state' gets modified
    let xmlData = xml.ToString()
    // HERE the put request
    Http.RequestString(url = credentials.Api + "/orders", 
                       headers = [ auth; 
                                   "Content-Type","text/xml" ],         
                       httpMethod= HttpMethod.Put,
                       body= HttpRequestBody.TextRequest(xmlData))

PUT请求的变化也不起作用,我在此处将请求正文从TextRequest更改为FormValues

Http.RequestString(url = credentials.Api + "/orders", 
                   headers = [ auth; 
                               "Content-Type","text/xml" ],         
                   httpMethod= HttpMethod.Put,
                   body= HttpRequestBody.FormValues ["xml", xmlData]) // xml=xmlData

我尝试的另一件事是将id添加到网址(即使是文档,他们说这不是必需的):

Http.RequestString(url = credentials.Api + "/order/" + orderId.ToString(), // added the id to the url
                   headers = [ auth; 
                               "Content-Type","text/xml" ],         
                   httpMethod= HttpMethod.Put,
                   body= HttpRequestBody.FormValues ["xml", xmlData]) // xml=xmlData

具体来说,我要更新订单的current_state节点的值。获取数据并对其进行修改可以按预期工作,但发送修改后的数据似乎不起作用,我仍然收到404: Not found错误

对此的任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:0)

好的,我刚刚用评论中给出的libraryexample对其进行了测试,我也使用相同的正面结果重复使用CURL的相同请求,因此没有特定的PHP语言。 我认为你需要在你的应用程序中重复相同的Headers / Body。

HTTP REQUEST HEADER
PUT /16011/api/orders/8 HTTP/1.1
Authorization: Basic TlpCUEJKTkhaWFpFMzlCMVBDTkdTM1JQN0s2NTVVQ0Y6
Host: localhost
Accept: */*
Content-Length: 2411
Content-Type: application/x-www-form-urlencoded

XML SENT
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<order>
    <id>8</id>
    <id_address_delivery xlink:href="http://localhost/16011/api/addresses/5">5</id_address_delivery>
    <id_address_invoice xlink:href="http://localhost/16011/api/addresses/5">5</id_address_invoice>
    <id_cart xlink:href="http://localhost/16011/api/carts/8">8</id_cart>
    <id_currency xlink:href="http://localhost/16011/api/currencies/1">1</id_currency>
    <id_lang xlink:href="http://localhost/16011/api/languages/1">1</id_lang>
    <id_customer xlink:href="http://localhost/16011/api/customers/2">2</id_customer>
    <id_carrier xlink:href="http://localhost/16011/api/carriers/3">3</id_carrier>
    <current_state xlink:href="http://localhost/16011/api/order_states/2" notFilterable="true">10</current_state>
    <module>bankwire</module>
    <invoice_number>0</invoice_number>
    <invoice_date>0000-00-00 00:00:00</invoice_date>
    <delivery_number>0</delivery_number>
    <delivery_date>0000-00-00 00:00:00</delivery_date>
    <valid>0</valid>
    <date_add>2015-09-17 08:29:17</date_add>
    <date_upd>2015-10-20 03:45:13</date_upd>
    <shipping_number notFilterable="true"></shipping_number>
    <id_shop_group>1</id_shop_group>
    <id_shop>1</id_shop>
    <secure_key>45838497c9182b0d361473894092de02</secure_key>
    <payment>Bank wire</payment>
    <recyclable>0</recyclable>
    <gift>0</gift>
    <gift_message></gift_message>
    <mobile_theme>0</mobile_theme>
    <total_discounts>0.000000</total_discounts>
    <total_discounts_tax_incl>0.000000</total_discounts_tax_incl>
    <total_discounts_tax_excl>0.000000</total_discounts_tax_excl>
    <total_paid>24.450000</total_paid>
    <total_paid_tax_incl>24.450000</total_paid_tax_incl>
    <total_paid_tax_excl>23.510000</total_paid_tax_excl>
    <total_paid_real>0.000000</total_paid_real>
    <total_products>16.510000</total_products>
    <total_products_wt>17.170000</total_products_wt>
    <total_shipping>7.280000</total_shipping>
    <total_shipping_tax_incl>7.280000</total_shipping_tax_incl>
    <total_shipping_tax_excl>7.000000</total_shipping_tax_excl>
    <carrier_tax_rate>4.000</carrier_tax_rate>
    <total_wrapping>0.000000</total_wrapping>
    <total_wrapping_tax_incl>0.000000</total_wrapping_tax_incl>
    <total_wrapping_tax_excl>0.000000</total_wrapping_tax_excl>
    <round_mode>2</round_mode>
    <conversion_rate>1.000000</conversion_rate>
    <reference>ECHCBFWGR</reference>
<associations></associations>
</order>
</prestashop>
相关问题