0

I am making a curl post request to create an order header... This returns and order number $orderno. When I echo the response, I am getting the order number as expected. I then run another post request to add order lines and one of the parameters is the order number that was generated when making order header. When i run the request, I notice that the order number parameter is "0" instead of (for example) 55235. Can someone explain to me why this is happening? Here is my code:

Again, when I echo response from first request, it returns 55235, but when i use as var in second request, it returns error saying that $orderno is 0. Therefore there is no order to add the order lines to.

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
      <AddOrderHeader xmlns="' . $pUrl . '">
        <contactid>'. $contactID .'</contactid>
        <shipvia>UPS</shipvia>
        <termscode>001</termscode>
        <custpo>' . $custpo . '</custpo>
        <billname>' . $billname . '</billname>
        <billaddr1>' . $billaddr1 . '/billaddr1>
        <billaddr2>' . $billaddr2 . '</billaddr2>
        <billcity>' . $city . '</billcity>
        <billstate>' . $state . '</billstate>
        <billzip>' . $zip . '</billzip>
        <billcntry>USA</billcntry>
        <shipname>' . $shipname . '</shipname>
        <shipaddr1>' . $shipaddr1 . '</shipaddr1>
        <shipaddr2>' . $shipaddr2 . '</shipaddr2>
        <shipaddr3>' . $shipaddr3 . '</shipaddr3>
        <shipcity>' . $order['customer']['shipping_address']['city'] . '</shipcity>
        <shipstate>' . $order['customer']['shipping_address']['state'] . '</shipstate>
        <shipzip>'. $order['customer']['shipping_address']['zip_cide'] . '</shipzip>
        <shipcntry>' . $order['customer']['shipping_address']['country_iso_code'] . '</shipcntry>
        <freightamt>' . $shipcost . '</freightamt>
      </AddOrderHeader>
    </soap:Body>
  </soap:Envelope>',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: text/xml'
  ),
));

$orderno = curl_exec($curl);
echo curl_error($curl);
curl_close($curl);

echo $orderno;

echo $orderno returns - 55235

but when I run another request and use $orderno as var:

$line_ids = $order_line['order_line_id'];
$sku      = $order_line['offer_sku'];
$quantity = $order_line['quantity'];
$price    = $order_line['price_unit'];
$custno   = $order_line['product_title'];

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
      <AddOrderLine xmlns="' . $pUrl . '">
        <contactid>' . $contactID . '</contactid>
        <orderno>' . $orderno . '</orderno>
        <itemno></itemno>
        <manuno>CT</manuno>
        <qty>' . $quantity . '</qty>
        <price>' . $price . '</price>
        <comments></comments>
        <custitem>' . $custno . '</custitem>
      </AddOrderLine>
    </soap:Body>
  </soap:Envelope>',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: text/xml'
  ),
));

$response = curl_exec($curl);
echo curl_error($curl);

curl_close($curl);
echo $response;

it returns error saying that the order number 0 isnt valid.

jasone
  • 55
  • 7
  • why do i suspect the problem lies on the service you are calling instead on curl.. also, keep in mind, with `curlopt_returntransfer` set to `true`, curl will return the result if any otherwise it returns `false` (0). – Bagus Tesa May 06 '22 at 15:39
  • @BagusTesa the first request is getting returned. Ex. echo $orderno… but when I use $orderno in second request, it is 0 – jasone May 06 '22 at 15:47
  • That's not how variables work, so you must be doing something wrong. Are these pieces of code in the same file? Also, that's really not how to build an XML request; the first unescaped character is going to blow the whole thing up. You should use proper XML tools to build the request payload. Did you know PHP has a built-in SOAP client class? – miken32 May 06 '22 at 16:31
  • It's difficult to help without seeing all the code. You can try to verify before the second curl the value of $orderno to be sure it's not set to 0. It's maybe a problem with the type of data. What is expected (string, integer... ) ? then force with the type needed : (interger) $orderno, (string) $orderno... – svgta May 06 '22 at 16:31
  • @miken32 this is directly from postman.. What do you mean properly build an xml request. Are you suggesting that Postman is incorrect? – jasone May 06 '22 at 16:54
  • @svgta I have verified that $orderno is returned before second request. It is an integer... I have tried to force with echo int($orderno). This returns nothing. I tried echo intval($orderno). This returns 0. echo $orderno returns 55235 as expected... but using in second requests fails. – jasone May 06 '22 at 16:59
  • I'm not suggesting anything, I'm *telling* you something. XML is not plain text and you can't just drop variables into it, as your code is doing. The correct way to do this is using one of the `DOMDocument`, `SimpleXML`, or `XMLWriter` classes. [For example...](https://stackoverflow.com/a/487629/1255289) – miken32 May 06 '22 at 17:00
  • @ miken32 all the variables are working fine as in code above EXCEPT the $orderno var. If it wasn't, the first request would not create and return an order number ($orderno) – jasone May 06 '22 at 17:16
  • @ miken32 btw... yes, I know PHP has a built in SOAP client... but that wasn't my question.... – jasone May 06 '22 at 17:32
  • @miken32 just to clarify… since you are TELLING me that I cannot pass a var as I’ve done, then EVERYONE who does this must be wrong? https://stackoverflow.com/a/12499739/9064035 – jasone May 07 '22 at 13:41

0 Answers0