1

我正在尝试使用即发即弃的方法向远程服务器发送请求。这是我的代码:

function backgroundPost($url, $data = array()){
  $parts=parse_url($url);

  $fp = fsockopen($parts['host'],
          isset($parts['port'])?$parts['port']:80,
          $errno, $errstr, 30);

  if (!$fp) {
      return false;
  } else {
      $encoded_data = json_encode($data);

      $output = "POST ".$parts['path']." HTTP/1.1\r\n";
      $output .= "Host: ".$parts['host']."\r\n";
      $output .= "Content-Type: application/json\r\n";
      $output .= "Content-Length: " . strlen($encoded_data) . "\r\n";
      $output .= "Connection: Close\r\n\r\n";
      $output .= $encoded_data;

      fwrite($fp, $output);
      fclose($fp);
      return true;
  }
}

//Example of use
backgroundPost('url-here', array("foo" => "bar"));

但是到达的数据只是空的。

当我在本地启动应用程序并将请求发送到我自己的机器时,数据确实到达了。

我对这种模式有误解吗?

为什么向我自己的机器发送请求而不是远程机器时它可以工作?

谢谢!

4

0 回答 0