php curl cookies

 

 

<?php

$PostData = array(
               'zipCode' => '12345',
               'langId'  => '-1',
               'storeId' => '10001',
               'URL'     => '/Brother-TN-580-Toner-Cartridge-High-Yield/product_612784',
               'errorUrl' => 'zipcode'
              );
$ua = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)';
$url = 'http://www.staples.com/Brother-TN-580-Toner-Cartridge-High-Yield/product_612784';
$host = $_SERVER['DOCUMENT_ROOT'];

if ( $curl = curl_init() )
{
   curl_setopt($curl, CURLOPT_URL, 'http://www4.staples.com/eluminate?ci=90028341&st=1255528609146&vn1=4.0.23B&ec=ISO-8859-1&pi=zipcode&tid=8&ti=1255528737238&hr=javascript%3AformSub%28addZipCodeForm%29%3B');
   curl_setopt($curl, CURLOPT_COOKIE, 'CoreID6=30201255514912193294815;Path=/;Domain=www4.staples.com;expires=Mon, 13-Oct-24 13:08:16 GMT');
   curl_setopt($curl, CURLOPT_COOKIE, 'TestSess3=x;Path=/;Domain=www4.staples.com;expires=End Of Session');
   curl_setopt($curl, CURLOPT_COOKIE, '90028341_login=1255528626018461671490028341;Path=/;Domain=www4.staples.com;expires=End Of Session');
   curl_setopt($curl, CURLOPT_COOKIE, '90028341_reset=1255528626;Path=/;Domain=www4.staples.com;expires=End Of Session');
  
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  
   curl_exec($curl);
   curl_close($curl);
} else {
   die('1 fails:' . curl_error($curl));
}

unset($curl);

//get cookie for sent
if ( $curl = curl_init($url) )
{  
   curl_setopt($curl, CURLOPT_HEADER, 1);
   curl_setopt($curl, CURLOPT_NOBODY, 1);
   curl_setopt($curl, CURLOPT_USERAGENT, $ua);
  
   ob_start();
  
   curl_exec($curl);
   curl_close($curl);
  
   $headers = explode("/n", ob_get_contents());
   ob_end_clean();
   /* TO DO: if $headers == '' error */
   foreach ($headers as $key => $value)
   {
      if ( stripos($value, 'Set-Cookie:') !== FALSE )
      {
         list($field, $cookie[]) = explode(' ', $headers[$key]);
      }
   }
     
   //echo '<pre>';
   //print_r($headers);
   //print_r($cookie);
  
   /* TO DO: if $cookie == '' error */
} else {
   die('2 fails:' . curl_error($curl));
}

unset($curl);

if( $curl = curl_init($url) )
{     
   curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE);
   curl_setopt($curl, CURLOPT_COOKIEFILE, $host . 'cookiefile');
  
   // responce in variable not in flow
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
   curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
  
   curl_setopt($curl,CURLOPT_ENCODING, 'gzip,deflate');
   curl_setopt($curl,CURLOPT_USERAGENT, $ua);
  
   if( $html = curl_exec($curl) )
   {
      curl_setopt($curl, CURLOPT_URL, 'http://www.staples.com/office/supplies/StaplesZipCodeAdd?');
      curl_setopt($curl, CURLOPT_POST, TRUE);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $PostData);
     
      for ($i=0; $i<sizeof($cookie); $i++)
      {
         curl_setopt($curl, CURLOPT_COOKIE, $cookie[$i]);
      }
      curl_setopt($curl, CURLOPT_COOKIE, $host . 'cookiefile');     
   }
curl_close($curl);
} else {
   die('3 fails:' . curl_error($curl));
}

unset ($curl);
unset ($html);

if ( $curl = curl_init() )
{
   curl_setopt($curl, CURLOPT_URL, 'http://www.staples.com/Brother-TN-580-Toner-Cartridge-High-Yield/product_612784?errorUrl=zipcode&storeId=10001&zipCode=12345&jspStoreDir=Staples&langId=-1&ddkey=StaplesZipCodeAdd');
   for ($i=0; $i<sizeof($cookie); $i++)
   {
      curl_setopt($curl, CURLOPT_COOKIE, $cookie[$i]);
   }
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   $html = curl_exec($curl);
   curl_close($curl);
} else {
   die('4 fails:' . curl_error($curl));
}

echo $html;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值