用PHP向伺服器傳送HTTP的POST請求,程式碼如下:
<?php/** * 傳送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", "content" => $postdata, "timeout" => 15 * 60 // 超時時間(單位:s) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; }
使用的時候直接呼叫上面定義的send_post方法:
$post_data = array( "username" => "username", "password" => "password");send_post("網址", $post_data);
用PHP向伺服器傳送HTTP的POST請求,程式碼如下:
<?php/** * 傳送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", "content" => $postdata, "timeout" => 15 * 60 // 超時時間(單位:s) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; }
使用的時候直接呼叫上面定義的send_post方法:
$post_data = array( "username" => "username", "password" => "password");send_post("網址", $post_data);