php發送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;
}
//測試
/*$post_data = array(
  	'username' => 'makalo',
  	'password' => 'makelochen'
);
echo send_post('http://localhost:8080/test.php', $post_data);*/
?>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM