php向指定网页发送post数据并获取网页内容

/**
 * file_get_contents版本
 * 发送post请求
 * @param string $url 请求地址
 * @param array $post_data post键值对数据
 * @return string
 */

function send_post($url, $post_data) {

    $postdata = http_build_query($post_data);
    $options = array(
        'http' =>array(
            'method' =>'POST',
            'header' =>'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'.'Referer: https://yabook.org/post/'.$post_data.'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'.'Host: yabook.org'.'Origin: https://yabook.org',
            'content' =>$postdata,
            'timeout' =>15 * 60 // 超时时间(单位:s)
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    return $result;
}
//使用方法
$id=$_GET['id'];
$post_data = array(
    'id' => "$id.'html'",
);
echo $theContent = send_post('https://yabook.org/e/DownSys/GetDown/ajax.php', $post_data);
Posted in PHP