A first example:
Call an interface for a weather forecast
$data= ' Thecityname= Shijiazhuang ';$CURL=curl_init (); curl_setopt ($CURL, Curlopt_url, "Http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName"); curl_setopt ($CURL, Curlopt_header, 0); curl_setopt ($CURL, Curlopt_returntransfer, 1); curl_setopt ($CURL, Curlopt_post, 1); curl_setopt ($CURL, Curlopt_useragent,$_server[' Http_user_agent ']); curl_setopt ($CURL, Curlopt_postfields,$data); curl_setopt ($CURL, Curlopt_httpheader,Array("application/x-www-form-urlencoded; Charset=utf-8 "," Content-length: ".strlen($data)));$output= Curl_exec ($CURL);if(!curl_errno ($CURL)) { Echo $output;}Else{ Echo"Curl error:". Curl_error ($CURL);}//Echo str_replace ("Baidu", "^_^", $output);Curl_close ($CURL);
A second example:
Simulate logging in to a site and saving a page
$data= ' name=admin&password=123456 ';$CURL=curl_init (); curl_setopt ($CURL, Curlopt_url, "http://www.***.com/a/login.html"); curl_setopt ($CURL, Curlopt_returntransfer, 1);d Ate_default_timezone_set (' PRC '); curl_setopt ($CURL, Curlopt_cookiesession,TRUE); curl_setopt ($CURL, Curlopt_cookiefile, "Cookiefile"); curl_setopt ($CURL, Curlopt_cookiejar, "Cookiefile"); curl_setopt ($CURL, Curlopt_cookie,Session_name()." =".session_id()); Curl_setopt ($CURL, Curlopt_header, 0); curl_setopt ($CURL, Curlopt_followlocation, 1); curl_setopt ($CURL, Curlopt_post, 1);//curl_setopt ($CURL, curlopt_useragent, $_server[' http_user_agent ');curl_setopt ($CURL, Curlopt_postfields,$data); curl_setopt ($CURL, Curlopt_httpheader,Array("application/x-www-form-urlencoded; Charset=utf-8 "," Content-length: ".strlen($data)) ; Curl_exec ($CURL); curl_setopt ($CURL, Curlopt_url, "http://www.***.com/a/analysisindex.html");//page to save curl_setopt ($CURL, Curlopt_post, 0); curl_setopt ($CURL, Curlopt_httpheader,Array("Content-type:text/xml"));$output= Curl_exec ($CURL); Curl_close ($CURL);Echo $output;
Two examples of using curl in PHP