Curl Post,file_get_contents Post,curl file_get_contents POST request data
In PHP, curl, file_get_contents functions can get remote link data, but file_get_contents is not very good control, for a variety of complex data acquisition scenarios, file_get_ Contents seems a little powerless, curl in the complex environment of data acquisition scenarios, a slight advantage. Curl function of the curl_setopt there are many parameters, the reader can take time to see the whole, although not usually used, but at least to do the heart bottom, know what parameters, if necessary, can be found to use. The basic use of file_get_contents function and curl function is briefly introduced in this paper:
Curl Post way to get data
Call Example
$post _data = Array ("category" => "9");
echo postcurl (' http://fity.cn/category.php ', $post _data);
Curl function--post the way to request resources
function Postcurl ($api _url, $post _data) {
$ch = Curl_init (); Initialize Curl handle
curl_setopt ($ch, Curlopt_url, $api _url); Set the URL address of the access
curl_setopt ($ch, Curlopt_timeout, 35); Set timeout
curl_setopt ($ch, Curlopt_connecttimeout, 15); Wait time, if set to 0, then do not wait
curl_setopt ($ch, Curlopt_header, false); Set whether to output page content
curl_setopt ($ch, Curlopt_returntransfer, true); Set whether to display header information
curl_setopt ($ch, Curlopt_post, true); Post data
curl_setopt ($ch, Curlopt_postfields, $post _data);//Post's variable
curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 5.1; SV1. NET CLR 1.1.4322;. NET CLR 2.0.50727); Simulate Browser header information
curl_setopt ($ch, Curlopt_referer, "http://www.x.com"); Forged Source Address
$data = curl_exec ($ch);
Curl_close ($ch);
if ($data) {
return $data;
} else {
return false;
}
}
File_get_contents Post way to get data
$postdata = Array (' category ' => 9);
$postdata = Http_build_query ($postdata);
$opts = Array (
' http ' => array (
' Method ' => ' POST ',
' Content ' => $postdata
)
);
$context = Stream_context_create ($opts);
$html = file_get_contents (' http://fity.cn/category.php ', false, $context);
Echo $html;