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);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2674baab6a35e37eea2b350c46aada15.html