php发送post请求的两种方法
header("Content-type: text/html; charset=utf-8");
/**
* 发送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(
'key' => 'c2fb4c2f7292861242a282a23cd08eb3',
'info' => '你好'
);
$rs=send_post('http://www.tuling123.com/openapi/api', $post_data);
echo $rs."<br/>";
/**
* Curl版本
* 使用方法:
* $post_string = "&app=request&version=beta";
* request_by_curl('', $post_string);
*/
function request_by_curl($remote_server, $post_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$post_string = "&key=c2fb4c2f7292861242a282a23cd08eb3&info=新闻";
$rs=request_by_curl('http://www.tuling123.com/openapi/api', $post_string);
echo $rs;
版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。
本文链接:php发送post请求的两种方法 - https://wziyi.com.cn/?post=56