LoginSignup
50

More than 5 years have passed since last update.

file_get_contentsでPOSTデータ送信

Posted at
    private function __post_ file_get_contents()
    {
        $url = 'http://qiita.com/';

        // POSTデータ
        $data = array(
            "param1" => "1",
            "param2" => 2
        );
        $data = http_build_query($data, "", "&");

        // header
        $header = array(
            "Content-Type: application/x-www-form-urlencoded",
            "Content-Length: ".strlen($data)
        );

        $context = array(
            "http" => array(
                "method"  => "POST",
                "header"  => implode("\r\n", $header),
                "content" => $data
            )
        );

        $html = file_get_contents($url, false, stream_context_create($context));

        return html;
    }

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50