使用curl在同一会话中发送http请求

时间:2014-12-10 07:56:42

标签: php curl

我正在使用Curl在PHP中开发我的第一个应用程序。此应用程序的目的是开发一个将数据发送到服务器的HTTP客户端。

服务器在@ 192.168.1.110上侦听端口10000。

这是服务器代码:

<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();

$address = '192.168.1.110';
$port = 10000;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}

//clients array
$clients = array();

do {
    $read = array();
    $read[] = $sock;
    $write = NULL;
    $except = NULL;

    $read = array_merge($read,$clients);

    // Set up a blocking call to socket_select
    if(socket_select($read,$write, $except, $tv_sec = 5) < 1)
    {
        continue;
    }

    // Handle new Connections
    if (in_array($sock, $read)) {       

        if (($msgsock = socket_accept($sock)) === false) {
            echo "socket_accept() fail: " . socket_strerror(socket_last_error($sock)) . "\n";
            break;
        }
        $clients[] = $msgsock;
        $key = array_keys($clients, $msgsock);
        /* Enviar instrucciones. */
        $msg = "HTTP/1.1 100 Continue \n";
        socket_write($msgsock, $msg, strlen($msg));

    }

    // Handle Input
    foreach ($clients as $key => $client) { // for each client       
        if (in_array($client, $read)) {
            if (false === ($buf = socket_read($client, 4096))) {
                echo "socket_read() falló: razón: " . socket_strerror(socket_last_error($client)) . "\n";
                break 2;
            }
            if (!$buf = trim($buf)) {
                continue;
            }
            echo "$buf\n";

            $talkback = "Client {$key}: msg '$buf'.\n";
            socket_write($client, $talkback, strlen($talkback));
            //echo "$buf\n";
        }

    } 
} while (true);

socket_close($sock);

这是HTTP客户端的源代码:

<?php
$url = 'http://192.168.1.110:10000';
$fields_string = "A";
$fields = array(
    'SERIAL_NUMBER ' => urlencode('123456'),
    'HARDWARE_ID ' => urlencode('455FFG'),
    'CODE ' => urlencode('99'),
    //field goes here 
                );

foreach($fields as $key=>$value) { 
    $fields_string .= $key.'='.$value.'&'; 
}
rtrim($fields_string, '&');



$ch = curl_init("http://192.168.1.110:10000");
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
crl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);                                                                     
$output = curl_exec($ch);
echo $output;
curl_setopt($ch,CURLOPT_POSTFIELDS,'test second session !!!!!');
$output = curl_exec($ch);
echo $output;
curl_close($ch);

当我执行这个应用程序(客户端/服务器)时,我得到了这个输出:

HTTP/1.1 100 Continue
Client 16: msg 'POST / HTTP/1.1
Host: 192.168.1.110:10000
Accept: */*
Content-Length: 52
Content-Type: application/x-www-form-urlencoded

ASERIAL_NUMBER =123456&HARDWARE_ID =455FFG&CODE =99&1HTTP/1.1 100 Continue
Client 17: msg 'POST / HTTP/1.1
Host: 192.168.1.110:10000
Accept: */*
Content-Length: 25
Content-Type: application/x-www-form-urlencoded

test second session !!!!!

问题是HTTP客户端不在同一会话中发送数据,客户端号码递增[Client 16,Client 17] !!

如何使用Curl在同一会话中发送数据?

1 个答案:

答案 0 :(得分:0)

会话存储在浏览器的cookie中:

Browser cookie

当你发出服务器端的Curl请求时(比如在你的PHP代码中),cookie没有被复制。

您需要做的是告诉Curl包含一个cookie标头。您从$_COOKIE数组中获取当前cookie,并将其放入您与Curl请求一起发送的标题中。

$headers = array();

// Add cookies to headers, if they're present
if(     isset($_COOKIE)     === true 
    &&  is_array($_COOKIE)  === true
    &&  sizeof($_COOKIE)    >   0
) {
    $cookie_header = "Cookie:";

    // Concatenate each cookie key and value like "key=value"
    foreach($_COOKIE as $k=>$v) {
        $cookie_header .= " ".safestring($k)."=".safestring($v).";";
    }

    $headers[] = $cookie_header;
}

// Add headers to request, if they're present
if(sizeof($headers) > 0) {
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}