0

我假装将 a 传递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.phpUndefined index:running中获取我需要的 SESSION 变量。那么,我错过了什么?如何检查文件cookie.txt是否已成功创建?谢谢

4

0 回答 0