Search code examples
phparraysfsockopen

php fsockopen how to return array


I have a site in codeigniter where I would lie to apply multithreading function. I have many function that simultaneously need to works together.
I have create a function with fsockopen but it doesn't return the array that I want to retrieve.
Inside my function action the dump of the array show the right array but when return in background_post I can't see it.
this is my code simplified:

    public function getFunction(){
       $params = array(
          "search_nation" => $search_nation,
       );
       $return = $this->background_post(site_url()."/controller/function", $params);
    }

    public function background_post($url, $data = array(), $debug = true) {
      $parts = parse_url($url);
      $host = $parts['host'];
      if (isset($parts['port'])) {
        $port = $parts['port'];
      }
      else {
        $port = $parts['scheme'] == 'https' ? 443 : 80;
      }
      if ($port == 443) {
        $host = 'ssl://' . $host;
      }

      $data_params = array();
      foreach ($data as $k => $v) {
        $data_params[] = urlencode($k) . '=' . urlencode($v);
      }
      $data_str = implode('&', $data_params);

      $fp = fsockopen(
        $host,
        $port,
        $errno,
        $errstr,
        3
      );

      if (!$fp) {
        return "Error $errno: $errstr";
      }
      else {
        $out = "POST " . $parts['path'] . " HTTP/1.1\r\n";
        $out .= "Host: " . $parts['host'] . "\r\n";
        $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
        if (!empty($data_str)) {
          $out .= "Content-Length: " . strlen($data_str) . "\r\n";
        }
        $out .= "Connection: Close\r\n\r\n";
        if (!empty($data_str)) {
          $out .= $data_str;
        }
        fwrite($fp, $out);
//--------------------HERE I WOULD LIKE TO SEE MY ARRAY THAT RETURN---------------
        echo'<p>---</p>';
        echo stream_get_contents($fp);
        echo'<p>---</p>';

        fclose($fp);
      }
    }

inside controller I have this funciton for example:

public function action(){
    $arr = $this->getSomething();
    var_dump($arr);
    return($arr);
}

This is what print code stream_get_contents($fp)

HTTP/1.1 200 OK Date: Thu, 27 Feb 2014 09:11:05 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.4 Set-Cookie: cisession=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%225b0efd5d2abbea594c220121df8c3aaa%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%22109.168.52.206%22%3Bs%3A10%3A%22user_agent%22%3Bb%3A0%3Bs%3A13%3A%22last_activity%22%3Bi%3A1393492265%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D42e22fbe9051d206b547b7ba7174f7c6; expires=Fri, 28-Feb-2014 09:04:25 GMT; path=/ Vary: Accept-Encoding Content-Length: 0 Connection: close Content-Type: text/html

Solution

  • There is something wrong with your endpoint script, it doesn't seem to be returning content, hence the header being returned with content-length: 0

    Check your end point is outputting what you want it to output.