About PHP's Curl series of functions, here's how they work.
In HTML, we can set HTTP POST and get submissions via form, but what if the data we get is not from the HTML, but rather the PHP script commits to the other server? At this time, how do we implement post and get commit data? The answer is PHP's curl function or Stream_context_create function, as well as the Fsockopen function, and so on, here about Curl's commit settings, the other two have time to write.
The function of Curl implements HTTP commit, the key is in four functions:
One is: Curl_init ()
Function: Initialize curl's ' socket stream '
The second one is: curl_setopt ()
Role: Set HTTP commit parameters
The third one is: Curl_exec ()
Role: Perform the commit of curl ' socket stream ' and get the content returned by the server
Fourth one: Curl_close ()
Function: Close the ' socket stream ' that has implemented the HTTP commit purpose
Said to say four functions, but the actual application, in fact, is to set the parameters of the function curl_setopt, the other three are extras, hehe.
Don't believe, let's take an example to use the Curl Series function to implement HTTP commit, not verbose, directly on the code:
<?PHP/** Create a curl_init commit function, simulate get commit, simulate post commit, simulate JSON commit * $url Submit server address, must * $data committed data, must, is associative array, * $method submission method, must, default get commit, Option values can only be filled in get, post, json* $options other options, can have no, if set, cannot set Curlopt_url, must conform to Curl_setopt_array () parameter syntax * If failed then return false, commit data succeeded , the results returned by the server are returned*/functionCurl$url= ",$data=Array(),$method= ' Get ',$options=Array()){ //Verify that data passed in data is legitimate if(Empty($url) || !filter_var ($url,Filter_validate_url)) { return false; } if(!Is_array($data) &&Empty($data)){ return false; } $method=Strtolower($method); if(!In_array($method,Array(' Get ', ' post ', ' JSON '))){ return false; } //Initializes a curl handle, $ch=Curl_init (); //setting the data returned by the server is not output directly, but remains in the return value of curl_exec ()curl_setopt ($ch, Curlopt_returntransfer, 1); if(!Empty($options) &&Is_array($options)){ if(Curl_setopt_array ($ch,$options) ==false){ return false; } } Switch($method){ Case' Get '://convert the data to be submitted to a get key-value pair commit $data=Http_build_query($data); curl_setopt ($ch, Curlopt_url,$url.‘?‘.$data); //gets the results that the server returns after Curl executes $return= Curl_exec ($ch); Curl_close ($ch); return $return; Break; Case' Post ':curl_setopt ($ch, Curlopt_url,$url); //set up post submissioncurl_setopt ($ch, Curlopt_post,true); //submit data for postcurl_setopt ($ch, Curlopt_postfields,$data); $return= Curl_exec ($ch); Curl_close ($ch); return $return; Break; Case' JSON '://set JSON-submitted data $data= Json_encode ($data); curl_setopt ($ch, Curlopt_url,$url); //set up post submissioncurl_setopt ($ch, Curlopt_post,true); //JSON data is submitted via postcurl_setopt ($ch, Curlopt_postfields,$data); $return= Curl_exec ($ch); Curl_close ($ch); return $return; Break; default:return false; Break; }} $url= "http://www.test.com/curl_setopt/upload.php"; //here is the test, I was tested successfully, my server is the direct output $_post,$_get $data=Array(' name ' = ' Foo ' ); $method= ' Post '; Var_dump(Curl ($url,$data,$method));
Note:: here::
JSON data submission, the server must be obtained by php://input, such as file_get_contents (' php://input ');
Examples of parameters for bool Curl_setopt_array (resource $ch, array $options):
Set the appropriate options for the curl socket stream $ch
$options = Array (
Curlopt_ssl_verifypeer = False,
Curlopt_header = False
);
The post and Json,curl_setopt_array functions cannot be set curlopt_postfields,curlopt_post these two items
Because I do not exclude this function, to exclude, you can modify the function yourself
The Curl_setopt_array function, which is actually the option of "bulk" setting the curl_setopt () function,
Their role is the same, I use it here, is to facilitate
The post submission can upload the file, but the address of the file must be set as "file" and the address is added @
Array (' file ' = ' @d:\vhost\test\curl_setopt\xingxing.jpg ')
Summarize:
If the above function you do not understand, it does not matter, the following I explain to you, the main meaning of the above function:
To implement the setup steps for HTTP submission using the Curl Socket stream:
First: Initialize Curl
This corresponds to the first step above:
// Initializes a curl handle, $ch = Curl_init ();
Second: Set the parameters of curl, that is, set the parameters of the HTTP commit, which corresponds to the second step above:
// convert the data to be submitted to a get key-value pair commit $data Http_build_query ($data); curl_setopt ($ch, Curlopt_url,$url. '? '. $data);
Http_build_query () This function is simply to format an associative array into a GET method to submit the URL key value pair form, this you can Baidu, here is unknown.
This step is the most critical place for curl to set up a link, and you can see the following post submission and JSON submission:
curl_setopt ($ch, Curlopt_url,$url); // Set POST submission curl_setopt ($ch, Curlopt_post,true); // submit the Post data curl_setopt ($ch, Curlopt_postfields,$data);
See no, setting up a post submission is actually setting the parameters in the curl_setopt function,
$ch is the curl_init initialized socket stream, the curl_setopt function is the constant setting of parameters in this $ch socket stream,
First parameter: Curlopt_url, which is a key parameter, which is the URL where the setting is to be submitted
Second parameter: curlopt_post, setting the way to submit POST
Third parameter: Curlopt_postfields, setting post submission data
Other parameters, I do not list here, if you need reference, you can link here: http://www.php.net/manual/zh/function.curl-setopt.php
The last two steps:
Step is: Curl_exec (), this is the execution Curl socket stream $ch
//Set the data returned by the server is not output directly, but remains in the return value of Curl_exec () curl_setopt ($ch, Curlopt_returntransfer, 1); $return = curl_exec ($ch);
Here to say, you can actually not set curlopt_returntransfer This parameter, if you do not set, then the server return value will be echo out, rather than save in the above $return
Another step is:
Curl_close ($ch);
This is the closing of the completion of the socket stream, lest it take up memory resources
Give two more functions:
Curl_errno ():
Returns the error number of the last curl operation.
Curl_error ():
Returns an error message for the most recent curl operation with explicit text.
These two functions can trace the error message of the curl_init socket stream, and can adjust the errors in the set curl process according to them.
To this end, there are questions can leave a message, there are errors, please point out, if you point out my mistake, I am grateful, personal insight short-sighted, wrong unavoidable, the eyes of the masses is discerning.
Detailed PHP's Curl function