Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
PHP
<pre><?php

$curl = curl_init();
$proId  = $_GET['id'];

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.australia-southeast1.gcp.commercetools.com/commerce-tool-project/carts/cart-id',
  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 =>'{
  "version" : 55,
  "actions" : [ {
    "action" : "addLineItem",
    "productId" : '.json_encode($proId).',
    "variantId" : 1,
    "quantity" : 1
  } ]
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer '.$acces_token
  ),
));

$response = curl_exec($curl);


echo $response;
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200){
  echo "Something went wrong! Try again";
      }
      else{
       header("Location: cart.php");
    
      }

      curl_close($curl);

      ?>



the error is
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\commerce tool new\Addtocart.php:33) in C:\xampp\htdocs\commerce tool new\Addtocart.php on line 38


What I have tried:

tried removing echo response but then it stoped working
Posted
Updated 22-Aug-21 23:35pm

1 solution

Move echo $response; inside the if branch:
PHP
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200){
  echo $response;
  echo "Something went wrong! Try again";
}
else {
  header("Location: cart.php");
}
If you're redirecting, virtually nobody would see the body of the response, so there's no point echoing it in the else branch.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900