'When getting response from Curl request, I get the correct response, but when using response as var in next request, it returns 0 Why?

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source