How to post data in PHP using file_get_contents?

前端 未结 3 1887
北恋
北恋 2020-11-22 01:05

I\'m using PHP\'s function file_get_contents() to fetch contents of a URL and then I process headers through the variable $http_response_header.

3条回答
  •  你的背包
    2020-11-22 01:18

    An alternative, you can also use fopen

    $params = array('http' => array(
        'method' => 'POST',
        'content' => 'toto=1&tata=2'
    ));
    
    $ctx = stream_context_create($params);
    $fp = @fopen($sUrl, 'rb', false, $ctx);
    if (!$fp)
    {
        throw new Exception("Problem with $sUrl, $php_errormsg");
    }
    
    $response = @stream_get_contents($fp);
    if ($response === false) 
    {
        throw new Exception("Problem reading data from $sUrl, $php_errormsg");
    }
    

提交回复
热议问题