用CURL传递会话

时间:2015-11-26 16:37:13

标签: php sockets session curl php-curl

我假装将COOKIE_FILE传递给另一个脚本,因为我正在使用php Curl进行一些请求,我需要一些SESSION变量,在我的主脚本中:

       define("COOKIE_FILE", "cookie.txt");
        $options = array(
            CURLOPT_HEADER => false,
            CURLOPT_TIMEOUT => 40000
        );

        $params=['Variable1'=>$initial, 'Variable2'=>$this->V2];
        $defaults = array(
            CURLOPT_URL => 'localhost/socket/src/myApp/socket2.php',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
            CURLOPT_COOKIEJAR => COOKIE_FILE,
            CURLOPT_COOKIEFILE => COOKIE_FILE,
            );
        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        curl_exec($ch);

这是调用socket2的脚本的完整代码:

<?php
namespace MyApp;
use funciones;
use Agente;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

define("COOKIE_FILE", "cookie.txt");
$_SESSION["running"] = time();

class Chat implements MessageComponentInterface {
    protected $clients;
    protected $data_clients;
    protected $target = "999999"; 


    public function __construct() {
        $this->clients = new \SplObjectStorage;
        $this->data_clients = new \SplObjectStorage;
        $this->send_message('testing from php');
        $this->Listen(true);
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);
        echo "New connection! ({$conn->resourceId})\n";
    }


    public function Listen($initial){
       $band=false;

       $options = array(
            CURLOPT_HEADER => true,
            CURLOPT_TIMEOUT => 40000
        );

        $params=['initial'=>$initial, 'target'=>$this->target'];
        $defaults = array(
            CURLOPT_URL => 'localhost/socket/src/myApp/socket2.php',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
            CURLOPT_COOKIEJAR => COOKIE_FILE,
            CURLOPT_COOKIEFILE => COOKIE_FILE,
            );

        //Tell cUrl about the cookie file
        //curl_setopt($ch,CURLOPT_COOKIEJAR, $this->cookieFile);  //tell cUrl where to write cookie data
        //curl_setopt($ch,CURLOPT_COOKIEFILE, $this->cookieFile); //tell cUrl where to read cookie data from

        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        curl_exec($ch);

        if(curl_errno($ch)){
            $band=true;
        }
        curl_close($ch);

        if(!$band)
            $this->Listen(false);
    }//fin Listen

    public function get_messages(){

        $band=false;
        $options = array(
            CURLOPT_HEADER => false,
            CURLOPT_TIMEOUT => 1000
        );

        $params=['method'=>'pollMessages'];
        $defaults = array(
            CURLOPT_URL => 'localhost/socket/src/myApp/ajax2.php',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
        );
        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        curl_exec($ch);

        if(curl_errno($ch)){
            $band=true;
        }
        curl_close($ch);

        if(!$band)
            $this->get_messages();
    }//fin de get messages

 public function send_message($mensaje){

        $band=false;
        $options = array(
            CURLOPT_HEADER => false,
        );

        $params=['method'=>'sendMessage','target'=>$this->target,'message'=>$mensaje];
        $defaults = array(
            CURLOPT_URL => 'localhost/socket/src/myApp/ajax2.php',
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
        );
        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        curl_exec($ch);
      /*  $headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT ); // request headers
    echo $headerSent;*/
        curl_close($ch);

    }//fin de enviar mensaje
在socket2.php中的

我有:

<?php
    require  '../../vendor/autoload.php';
    set_time_limit(10); //1 minute
    session_start();
    session_write_close();

    $time = $_SESSION["running"];
?>

我正在尝试读取变量正在运行 但我仍然在另一边( socket2.php Undefined index:running获取我需要的SESSION变量。那么,我错过了什么?我如何检查文件 cookie.txt 是否已成功创建?感谢

0 个答案:

没有答案