Describes several php curl functions and phpcurl functions.

Source: Internet
Author: User

Describes several php curl functions and phpcurl functions.

For a series of php curl functions, here we will explain their functions.

In html, we can set http post and get submissions through form. But what if the data we get is not from html, but is the php script actively committing data to other servers? At this time, how should we implement post and get to submit data? The answer is the php curl function, the stream_context_create function, and the fsockopen function. Here we will talk about the curl submission settings, and the other two will have time to write.

Curl functions implement http submission. The key lies in four functions:

One is: curl_init ()

Purpose: Initialize the curl ''connected stream''

The second is: curl_setopt ()

Purpose: Set http submission parameters.

The third is: curl_exec ()

Purpose: Execute the curl ''connected stream'' submission and obtain the content returned by the server.

Fourth: curl_close ()

Purpose: Disable ''connected stream'' that has achieved http submission''

Four functions are mentioned, but in actual applications, the parameter curl_setopt is actually set. The other three functions are all running.

I don't believe it. Here is an example to use the curl series functions to implement http submission. If you don't have to worry about it, go directly to the Code:

  

<? Php/** create a curl_init submission function, simulate get submission, simulate post submission, simulate json submission * $ url submitted server address, must be * $ data submitted data, required, is an associated array. * $ method is required for submission. By default, get is submitted. The option value can only be set to get, post, or json * $ options. Other options are optional, if it is set, you cannot set CURLOPT_URL. It must comply with the parameter Syntax of curl_setopt_array () * if it fails, false is returned. Data is submitted successfully, returns the result */function curl ($ url = '', $ data = array (), $ method = 'get', $ options = array () returned by the server ()) {// verify whether the data passed in is valid 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;} // initialize the curl handle, $ ch = curl_init (); // set the data returned by the server to be retained in the returned value of curl_exec () instead of directly outputting data, instead, 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 get key-value pairs and submit $ data = http_build_query ($ data); curl_setopt ($ ch, CURLOPT_URL, $ url. '? '. $ Data); // after obtaining the curl for execution, the server returns the result $ return = curl_exec ($ ch); curl_close ($ ch); return $ return; break; case 'post ': curl_setopt ($ ch, CURLOPT_URL, $ url); // you can specify curl_setopt ($ ch, CURLOPT_POST, true) for post submission. // you can specify curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); $ return = curl_exec ($ ch); curl_close ($ ch); return $ return; break; case 'json ': // set the data submitted in json format $ data = json_encode ($ data); curl_setopt ($ ch, CURLOPT_URL, $ url); // set the post commit curl_setopt ($ ch, CURLOPT_POST, true); // submit curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data) through post; $ return = curl_exec ($ ch); curl_close ($ ch ); return $ return; break; default: return false; break; }}$ url = "http://www.test.com/curl_setopt/upload.php"; // below is the test. I have succeeded in the test, my server directly outputs $ _ POST, $ _ GET $ data = array ('name' => 'foo'); $ method = 'post '; var_dump (curl ($ url, $ data, $ method ));

 

  

Note: here ::

Json data is submitted. The server must be obtained using php: // input, for example, file_get_contents ('php: // input ');

Bool curl_setopt_array (resource $ ch, array $ options) parameter example:


Set the curl set to stream $ ch corresponding options

$ Options = array (
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => false
);


The CURLOPT_POSTFIELDS and CURLOPT_POST parameters cannot be set for the post and json, curl_setopt_array functions.
Because I have not excluded this function. to exclude it, you can modify this function by yourself.

 

The curl_setopt_array function is actually the '''batch ''' option for setting the curl_setopt () function,

They play the same role. I use it here for convenience.


Post submission can upload files, but the file address must be set as follows. The subscript is 'file' and @ is added before the address @
Array ('file' => '@ D: \ vhost \ test \ curl_setopt \ xingxing.jpg ')
    

Summary:

If you cannot understand the above function, it doesn't matter. I will explain it to you as follows:

    

Follow these steps to configure http submission by using curl to intercept a stream:

First: Initialize curl

This corresponds to the first step above:

// Initialize the curl handle, $ ch = curl_init ();

Second, set the curl parameter, that is, set the http submission parameter, which corresponds to the second step above:

// Convert the data to get key-value pairs to submit $ data = http_build_query ($ data); curl_setopt ($ ch, CURLOPT_URL, $ url .'? '. $ Data );

 

The function http_build_query () is used to format an associated array into a url key-Value Pair submitted in get mode. You can use Baidu for this purpose, which is not detailed here.

This step is the most important part of curl settings. You can see the following post submission and json submission:

Curl_setopt ($ ch, CURLOPT_URL, $ url); // you can specify curl_setopt ($ ch, CURLOPT_POST, true) for post submission. // you can specify curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );

No. Setting post submission is actually setting parameters in the curl_setopt function,

$ Ch is the set stream initialized by curl_init. The curl_setopt function continuously sets parameters in the $ ch set stream,

The first parameter: CURLOPT_URL, which is a key parameter and specifies the url to be submitted.

The second parameter is CURLOPT_POST, which sets the submission method to Post.

The third parameter is CURLOPT_POSTFIELDS, which sets the post data submission.

Other parameters, I will 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 1: curl_exec (), which is to execute curl to overwrite the stream $ ch

// Set the data returned by the server to be retained in the returned value of curl_exec () instead of directly outputting data, curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); $ return = curl_exec ($ ch );

You can leave CURLOPT_RETURNTRANSFER Unspecified. If you do not set CURLOPT_RETURNTRANSFER, the server returns an echo instead of storing it in the $ return

Another step is:

curl_close($ch);

This is the completion of the execution of the stream, so that it does not occupy memory resources

Two more functions are provided:

Curl_errno ():

Returns the error code of the last cURL operation.

Curl_error ():

Returns an error message of the text specified by the last cURL operation.

These two functions can trace the error information of the curl_init stream, and adjust the errors during curl setting.

This is the end. If you have any questions, please leave a message. If you have any mistakes, I would be very grateful to you. My personal experience is short, and errors are inevitable, the eyes of the masses are bright.

 

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.