PHP Curl file_get_contents Post way to get data

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.