当前位置: 移动技术网 > IT编程>开发语言>PHP > php中运用http调用的GET和POST方法示例

php中运用http调用的GET和POST方法示例

2018年07月15日  | 移动技术网IT编程  | 我要评论

使用到的函数是curl_init, curl_setopt, curl_exec,curl_close。

默认是get方法,可以选择是否使用header:

$ch = curl_init();
curl_setopt($ch, curlopt_url, "$url");
curl_setopt($ch, curlopt_timeout, 2);
curl_setopt($ch, curlopt_header, 1); //如果设为0,则不使用header
curl_setopt($ch,curlopt_returntransfer,1);
$result = curl_exec($ch);
curl_close($ch);

post方法:

$ch = curl_init();
curl_setopt($ch,curlopt_url,'$url');
curl_setopt($ch,curlopt_post,1);
curl_setopt($ch,curlopt_returntransfer,true);
$vars =sprintf('from=%d&to=%d&subject=%s&body=%s',$from, $to, urlencode($subject), urlencode($body));
curl_setopt($ch,curlopt_postfields,$vars);
$ret = curl_exec($ch);
curl_close($ch);

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网