当前位置:首页 > PHP教程 > PHP高级教程

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

使用到的函数是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);

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!