Explain the logic of the curl process in php

I use PHP and parse the url from the website and I know that there are two methods for this file_file (url) and curl. I know that we have more features with curl, so I use this. But I just wanted to know the process behind the curl. How it works when we briefly make a twist request

+4
source share
3 answers

The PHP cURL package simply provides the cURL / libcurl API (which is written in C) in PHP. cURL is really useful for moving data across all types of protocols and has many good options. On the other hand, file_get_contents is one of the basic operations with PHP files, and it relies on the kernel to try and find the requested resource. Overall, cURL would be a better choice, although often it requires a few more lines of code. One of the problems with file_get_contents () is that in some cases the connection remains open after the request, so calling the function blocks the script until the request expires and you can see a lot of lag.

Literature:

http://php.net/manual/en/book.curl.php

http://en.wikipedia.org/wiki/CURL

http://www.php.net/manual/en/ref.filesystem.php

+4
source

curl is a client for receiving documents or files or sending documents to a server using any of the supported protocols (HTTP, HTTPS, FTP, GOPHER, DICT, TELNET, LDAP or FILE). The command is designed to work without any interaction with the user or with any or interactivity.

curl offers many useful tricks, such as proxy support, user authentication, ftp download, HTTP message, SSL (https :) connections, cookies, resume file transfers, and much more.

Example:

//set POST variables $url = 'http://domain.com/get-post.php'; $fields = array( 'lname'=>urlencode($last_name), 'fname'=>urlencode($first_name), 'title'=>urlencode($title), 'company'=>urlencode($institution), 'age'=>urlencode($age), 'email'=>urlencode($email), 'phone'=>urlencode($phone) ); //url-ify the data for the POST foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } rtrim($fields_string,'&'); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); //execute post $result = curl_exec($ch); //close connection curl_close($ch); 

For a better understanding, use the following links:

can it help you.

+3
source

Curl is mainly used to create a REST request.

here is a simple example for a mail request using curl

  $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,$GLOBAL_SMS_URL); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,20); curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); $getit = json_decode($buffer, true); 

now the first line of code is used to initialize the curl in the second line we define the remote url $ GLOBAL_SMS_URL (in my case)

Third line I determine the timeout in seconds

I pass the headers in the 4th line

one important thing if you want to go through the twisting body use this

  curl_setopt($curl_handle, CURLOPT_POSTFIELDS,$json); 

where $ json will contain the curl request body

or if you want to pass some parameter to the url

  $data = array( "Username" => "56y5768", "Pwd" => "tr54656y", "PhoneNumber" => $phone, "PhoneMessage" => $text ); $getdata = http_build_query($data) . "\n"; $GLOBAL_SMS_URL = $SMS_API_BASE_URL.$getdata; 

hope this helps

+1
source

Source: https://habr.com/ru/post/1432632/


All Articles