Use CURL to get cookie value and set cookie value

Discussion in 'PHP' started by adbox, Dec 11, 2009.

  1. #1
    Hey I am using curl to access a page that has iframes, and uses cookies.

    I need to access the index.php
    grab a cookie by its name

    then go to a page in the iframes with the cookie set in the header, or something like that.

    can anyone help?
     
    adbox, Dec 11, 2009 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
  3. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I made this function for you try it :

    function get_content($url)  
    { 
    	$ch = curl_init();  
    	curl_setopt($ch, CURLOPT_URL, $url);  
    	curl_setopt($ch, CURLOPT_HEADER, 1); 
    	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');
    	curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    	curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); //saved cookies
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	   
    	$string = curl_exec ($ch);  
    	curl_close ($ch);  
     
    	return $string;      
    }
    PHP:
     
    xenon2010, Dec 14, 2009 IP
  4. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #4
    thank you all, this will help for my documentation allot! thank you.
     
    adbox, Dec 14, 2009 IP
  5. echipvina

    echipvina Active Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    
    $ch = curl_init('http://www.google.com/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // get headers too with this line
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $result = curl_exec($ch);
    // get cookie
    preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
     
    parse_str($m[1], $cookies);
    var_dump($cookies);
     
    
    PHP:
     
    echipvina, Apr 24, 2013 IP