PHP实现file_get_contents发送post请求

最近按照客户需求写一个支付异步回调通知url的需求,本想用curl写的,后来想想用file_get_contens是否可以实现,果然可以

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$data = array(
        'name' => 'anderyly',
        'age' => '19',
        'grade' => 'boy',
    ); 
    $query = http_build_query($data); 
    
    $options['http'] = array(
        'timeout' => 60,
        'method' => 'POST',
        'header' => 'Content-type:application/x-www-form-urlencoded',
        'content' => $query
    );
    
    $url = "https://blog.aaayun/cc/";
    $context = stream_context_create($options);
    $result = @file_get_contents($url, false, $context);
Licensed under CC BY-NC-SA 4.0
最后更新于 Aug 15, 2025 15:01 +0800