Add to cart through PHP with cURL

General ShopSite user discussion

Add to cart through PHP with cURL

Postby ad_shipping » Tue Aug 25, 2009 11:58 am

I have to write a PHP script that recieves POST information when a customer clicks an "Add To Cart" button, alters some of the data, then forwards it to the shopping cart.

I have it working, but when I add a product to the cart the normal way (straight from the product page to the shopping cart), it creates a different instance of the shopping cart. I think it has something to do with cookies, but I've tried everything I can think of, and I can't get it to work.

I have been using cURL but I would try something different if it might work.

Thanks
ad_shipping
 
Posts: 2
Joined: Thu Aug 20, 2009 1:55 pm
Location: US

Postby ad_shipping » Tue Aug 25, 2009 12:32 pm

this code adds the product to shopping cart #1 but then displays shopping cart #2 (the one I want the product to go into):

(shopping cart #2 is the cart where the products go when they are added using the normal method)

Code: Select all
<?php

$cookieDataString = '';
foreach( $_COOKIE as $key=>$value ) {
   $cookieDataString .= $key.'='.$value.' ;';
}
$cookieDataString = rtrim($cookieDataString, ' ;');
//echo $cookieDataString;

$postDataString = '';
foreach( $_POST as $key=>$value )
   $postDataString .= $key.'='.$value.'&';
$postDataString = rtrim($postDataString, '&');

$username = '**********';
$password = '**********';

$ch = curl_init('http://www.activedogs.com/cgi-activedogs/sb/order.cgi');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:application/x-www-form-urlencoded"));
//If the target server returns a redirect request using the "Location:" header directive, then follow it.
//To prevent recursive redirects, only do a max of 5 follows
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_MAXREDIRS, 5);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataString);
curl_setopt($ch, CURLOPT_COOKIE, $cookieDataString);
$output = curl_exec($ch);
//$info = curl_getinfo($ch);
curl_close($ch);

header('Location: [-- SHOPPING_CART_URL --]');

?>
ad_shipping
 
Posts: 2
Joined: Thu Aug 20, 2009 1:55 pm
Location: US


Return to User Forum

Who is online

Users browsing this forum: No registered users and 120 guests