Plugin Directory

Changeset 1458486


Ignore:
Timestamp:
07/21/2016 07:12:28 PM (10 years ago)
Author:
clickfunnels.com
Message:

Send and receive cookies with curl request

File:
1 edited

Legend:

Unmodified
Added
Removed
  • clickfunnels/trunk/clickfunnels.php

    r1458485 r1458486  
    5656
    5757            $posts = get_posts($query_args);
    58             $cf_page = $posts[0];
     58            $cf_page = current($posts);
    5959
    6060            if ($cf_page) {
     
    9393    public function get_page_content( $url ) {
    9494        $ch = curl_init();
    95         $timeout = 10;
    9695        curl_setopt($ch, CURLOPT_URL, $url);
    97         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    98         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
     96        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     97        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    9998        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    100         $data = curl_exec($ch);
     99
     100        // Get http header for cookies
     101        curl_setopt($ch, CURLOPT_VERBOSE, 1);
     102        curl_setopt($ch, CURLOPT_HEADER, 1);
     103
     104        // Forward current cookies to curl
     105        $cookies = array();
     106        foreach ($_COOKIE as $key => $value) {
     107            if ($key != 'Array') {
     108                $cookies[] = $key . '=' . $value;
     109            }
     110        }
     111        curl_setopt( $ch, CURLOPT_COOKIE, implode(';', $cookies) );
     112
     113        // Stop session so curl can use the same session without conflicts
     114        session_write_close();
     115
     116        $response = curl_exec($ch);
    101117        curl_close($ch);
    102         return $data;
     118
     119        // Session restart
     120        session_start();
     121
     122        // Seperate header and body
     123        list($header, $body) = explode("\r\n\r\n", $response, 2);
     124
     125        // Extract cookies form curl and forward them to browser
     126        preg_match_all('/^(Set-Cookie:\s*[^\n]*)$/mi', $header, $cookies);
     127        foreach($cookies[0] AS $cookie) {
     128            header($cookie, false);
     129        }
     130
     131        return $body;
    103132    }
    104133
Note: See TracChangeset for help on using the changeset viewer.