无法通过php通过套接字连接发送对象数组

时间:2012-10-09 05:26:49

标签: php php-socket

我对php完全不熟悉。我有两页让A.php和B.php想要在页面A时运行Page B.因为我创建了一个Socket Connection并且能够运行Page B但问题是当我发送一个对象数组时页面B.page A只发送数组中列出的第一个对象和数组的其他值都没有 这是我的一些代码 在A.php中

$arr = array("message" => $pushData,
  "messageType" => $messageType,
  "displayGroupID" => $displayGroupID,
  "db"=>$db);
$fp1 = $this->JobStartAsync('localhost', 'B.php', $arr);
$r1 = $this->JobPollAsync($fp1);

function JobStartAsync($server, $url, $data, $port=80,$conn_timeout=10, $rw_timeout=86400)
{
    $errno = '';
    $errstr = '';
    $data = http_build_query($data);

    set_time_limit(0);

    $fp = fsockopen($server, $port, $errno, $errstr, $conn_timeout);

    if (!$fp)
    {
        echo "$errstr ($errno)<br />\n";
        return false;
    }

    $out = "POST $url HTTP/1.1\r\n";
    $out .= "Host: $server\r\n";
    $out .= "Content-type: application/x-www-form-urlencoded\r\n";
    $out .= "Content-length: ". strlen($data) ."\r\n";
    $out .= "Connection: Close\r\n\r\n";
    $out .= "$data";
    stream_set_blocking($fp, false);
    stream_set_timeout($fp, $rw_timeout);
    fwrite($fp, $out);
    //fwrite($fp, $data);
    return $fp;
}

function JobPollAsync(&$fp)
{
    if ($fp === false)
        return false;

    if (feof($fp))
    {
        fclose($fp);
        $fp = false;
        return false;
    }

    return fread($fp, 10000);
}

IN B.php

fileWrite ($_POST['displayGroupID'],$_POST['message'],$_POST['messageType']);

它只获得“消息”,因为它被列为数组中的第一个元素

1 个答案:

答案 0 :(得分:0)

看看CURL。这就是您所需要的,也是其他人使用的。

ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.example.com");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
        "postvar1=value1&postvar2=value2&postvar3=value3");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec ($ch);

curl_close ($ch);

对于异步变体,请查看之前回答的类似问题 - How do I make an asynchronous GET request in PHP?

相关问题