通常情況下,php發送請求是通過curl擴展實現的,那麼不通過curl是否可以直接發送請求?php已有了內置的函數來模擬POST/GET請求,即stream_context_create(),直接看demo吧!
protected function php_ajax()
{
$url = "http(s)://www.example.com";
$post_data= [
'appkey'=>'',
'channel'=>'',
'content'=>''
];//自定義內容
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60
)
);
$context = stream_context_create($options);
file_get_contents($url, false, $context);
}