无法通过 PHP 中的套接字连接通过 POST 发送对象数组



我对php完全陌生。我有 2 个页面让 A.php 和 B.php 想在 A 页面运行时运行页面 B.为此,我创建了一个套接字连接并能够运行页面 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.1rn";
    $out .= "Host: $serverrn";
    $out .= "Content-type: application/x-www-form-urlencodedrn";
    $out .= "Content-length: ". strlen($data) ."rn";
    $out .= "Connection: Closernrn";
    $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);
}

在B.php

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

它只得到"消息",因为它被列为数组中的第一个元素

看看 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);

对于异步变体,请查看之前回答的类似问题 - 如何在 PHP 中发出异步 GET 请求?

最新更新