How to post data in PHP using file_get_contents?

前端 未结 3 1888
北恋
北恋 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:14

    $sUrl = 'http://www.linktopage.com/login/';
    $params = array('http' => array(
        'method'  => 'POST',
        'content' => 'username=admin195&password=d123456789'
    ));
    
    $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");
    }
    

提交回复
热议问题