读书人

施用curl发送HTTP请求

发布时间: 2012-08-25 10:06:20 作者: rapoo

使用curl发送HTTP请求

public function request($requestbody){                //设置head,这里发送和接收的head类型是text/xml$headers_org = array(                       "Content-Type:text/xml;charset=UTF-8",                       "Accept:text/xml;charset=UTF-8",                       "Connection:Keep-Alive",                       "Expect:100-Continue");$user_agent = "Apache-HttpClient/4.0.1 (java 1.5)";$curl = curl_init();curl_setopt($curl,CURLOPT_URL, $this->getURL()); //设置请求URLcurl_setopt($curl, CURLOPT_HTTPHEADER, $headers_org); //设置请求头curl_setopt($curl, CURLOPT_POST, 1 ); //设置为post形式的请求curl_setopt($curl,CURLOPT_POSTFIELDS, $requestbody); //xml的bodycurl_setopt($curl, CURLOPT_RETURNTRANSFER,1); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);curl_setopt($curl, CURLOPT_TIMEOUT, 20);//设置超时时间,单位为s$output = curl_exec($curl); //这边的output就是返回的responsecurl_close($curl);}

?

curl的官方文档地址:http://www.php.net/manual/zh/book.curl.php

读书人网 >Web前端

热点推荐