Unable to send array of objects via POST over socket connection in php

356 views Asked by At

I am totally new to php. I had 2 pages let A.php and B.php want to run Page B while I am at Page A.For that I had created a Socket Connection and able to run The Page B but problem is that when I send an array of objects to page B.page A send only the First object listed in the array and other values of the array were nothing here is my some code in 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']);

it get only "message" as it is listed as 1st element in array

1

There are 1 answers

0
Mirko Adari On BEST ANSWER

Take a look at CURL. This is what you need and everyone else uses.

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

For async variation take a look at a similar question answered before - How do I make an asynchronous GET request in PHP?