How Can I Connect To A Tor Hidden Service Using Curl In Php
5 PHP cURL examples
Published September 06, 2015
Working on the server side does not necessarily imply that all the required information needs to exist present on the database of the site on which we are working. As a matter of fact, growing parts of the information that is used on the server side comes from external sources, often via an API that allows some kind of service to provide information to the website without having access to the database on the remote site. To utilize this data, we can use the cURL built-in PHP extension.
cURL is a PHP extension, that allows the states to receive and ship data via the URL syntax. By doing and so, cURL makes it easy to communicate betwixt unlike websites and domains. This tutorial includes 5 common cases for the use of curl, and they include:
# How does the coil extension work?
cURL works by sending a request to a web site, and this process includes the following iv parts:
1. Initialization.
$handle = curl_init();
2. Setting the options. There are many options, for example, an option that defines the URL.
curl_setopt($handle, CURLOPT_URL, $url);
3. Execution with curl_exec().
$information = curl_exec($handle);
4. Releasing the cURL handle.
curl_close($handle);
The second part is the most interesting because it allows united states of america to ascertain how curl works in a highly accurate mode, past using the many options it has to offer.
# 1. How to download the contents of a remote website to a local file?
In order to download the contents of a remote web site, nosotros need to define the post-obit options:
CURLOPT_URL- Defines the remote URL.
CURLOPT_RETURNTRANSFER- Enables the assignment of the data that nosotros download from the remote site to a variable. In this case, we assign the information into the variable $output.
<?php $handle = curl_init(); $url = "https://world wide web.ladygaga.com"; // Set the url curl_setopt($handle, CURLOPT_URL, $url); // Set the outcome output to be a cord. curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($handle); curl_close($handle); echo $output;
When we print the value of the variable $output to the screen, we will run across the local version of the web site for the earth's best vocalist.
The options can be written more than compactly using curl_setopt_array(), which is a curl part that convenes the options into an assortment.
curl_setopt_array($handle, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => truthful ) );
# 2. How to download a file from a remote site using cURL?
A remote file tin be downloaded to our server, if the selection CURLOPT_ FILE is set. For example, the post-obit code downloads the book "The Divine Comedy" from Project Gutenberg into a the_divine_comedy.html file on our server:
<?php // The distant site url. $url = "https://www.gutenberg.org/files/46852/46852-h/46852-h.htm"; // The file on our server. $file = __DIR__ . DIRECTORY_SEPARATOR . "the_divine_comedy.html"; $handle = curl_init(); // Open up the file on our server for writing. $fileHandle = fopen($file, "w"); curl_setopt_array($handle, array( CURLOPT_URL => $url, CURLOPT_FILE => $fileHandle, ) ); $information = curl_exec($handle); curl_close($handle); fclose($fileHandle);
# Handling the returned response
In society to get the parameters of the response for it to be monitored and debugged, we need to set the pick CURLOPT_HEADER. For case:
<?php $url = ""https://www.gutenberg.org/files/41537/41537-h/41537-h.htm"; $file = __DIR__ . DIRECTORY_SEPARATOR . "the_divine_comedy.html"; $handle = curl_init(); $fileHandle = fopen($file, "w"); curl_setopt_array($handle, array( CURLOPT_URL => $url, CURLOPT_FILE => $fileHandle,CURLOPT_HEADER => true ) ); $data = curl_exec($handle);
To become additional information about the asking, nosotros use the curl_getinfo control that enables united states of america to receive important technical information well-nigh the response, including the status lawmaking (200 for success) and the size of the downloaded file.
$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE ); $downloadLength = curl_getinfo($handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD );
In add-on, nosotros can also employ the commands: curl_error and curl_errno to debug the response and receive informative error letters.
if(curl_errno($handle)) { impress curl_error($handle); }
Allow'due south meet the full code:
<?php $url = "https://www.gutenberg.org/files/46852/46852-h/46852-h.htm"; $file = __DIR__ . DIRECTORY_SEPARATOR . "the_divine_comedy.html"; $handle = curl_init(); $fileHandle = fopen($file, "due west"); curl_setopt_array($handle, array( CURLOPT_URL => $url, CURLOPT_FILE => $fileHandle, CURLOPT_HEADER => true ) ); $data = curl_exec($handle); $responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); $downloadLength = curl_getinfo($handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD); if(curl_errno($handle)) { impress curl_error($handle); } else { if($responseCode == "200") echo "successful asking"; repeat " # download length : " . $downloadLength; curl_close($handle); fclose($fileHandle); }
# 3. How to submit forms with scroll?
Until this moment, we have demonstrated the use of the GET method of HTTP (which is by and large used to picket and download content). whorl tin can too make use of the Mail method of HTTP in club to submit forms.
In order to demonstrate course submission with ringlet, we need to create the post-obit two files:
- alphabetize.php in which nosotros put the ringlet script.
- form.php in which we put the grade to be submitted.
The form.php volition exist in reality, found on a remote server (although, for the sake of the example, both files may be placed on the same server). Besides, for the example, nosotros will employ a grade with 3 fields: firstName, lastName and submit.
<?php if(isset($_POST["submit"])) { repeat "Full name is " . $_POST["firstName"] . " " . $_POST["lastName"]; get out; } ?> <html> <body> <form method = "Mail service" action = "" > <input name="firstName" blazon="text"> <input proper name="lastName" type="text"> <input type="submit" name="submit" value="שלח" > </form> </body> </html>
To submit the grade, the post-obit options need to be set:
- CURLOPT_POST– Sets the request to be in a post mode.
- CURLOPT_POSTFIELDS- Receives the associative array of the fields that we want to post. The array keys are named after the name of the form fields.
<?php $handle = curl_init(); $url = "https://localhost/curl/theForm.php"; // Assortment with the fields names and values. // The field names should match the field names in the grade. $postData = array('firstName' => 'Lady','lastName' => 'Gaga','submit' => 'ok' ); curl_setopt_array($handle, assortment( CURLOPT_URL => $url, // Enable the mail service response.CURLOPT_POST => true,// The data to transfer with the response.CURLOPT_POSTFIELDS => $postData, CURLOPT_RETURNTRANSFER => true, ) ); $data = curl_exec($handle); curl_close($handle); echo $information;
# 4. How to perform basic HTTP authentication with curl?
In order to authenticate with cURL, the post-obit 3 options demand to be gear up:
- CURLOPT_HTTPAUTH
- CURLOPT_USERPWD– Through which we define the username and password.
- CURLOPT_RETURNTRANSFER
Let'due south see the code:
curl_setopt_array($handle, assortment( CURLOPT_URL => $url,CURLOPT_HTTPAUTH => CURLAUTH_ANY,CURLOPT_USERPWD => "$username:$countersign", CURLOPT_RETURNTRANSFER => true, ) );
# five. How to handle cookies with cURL?
The apply of cookies allows a website to identify returning visitors and authenticated users. To this finish, coil provides us with a mechanism through which we can save cookies.
The 2 main options that let u.s.a. to handle cookies are:
- CURLOPT_COOKIEJAR– Defines the file required to write the cookies.
- CURLOPT_COOKIEFILE– Defines the file from which the cookies are to exist read.
The post-obit code example writes the cookies into a cookie.txt file on the kickoff visit, and then reads the data in after visits.
<?php $handle = curl_init(); $url = "https://www.ladygaga.com/artrave-the-artpop-brawl"; $file = __DIR__ . DIRECTORY_SEPARATOR . "cookie.txt"; curl_setopt_array($handle, array( CURLOPT_URL => $url, // The file to which the cookies demand to be written.CURLOPT_COOKIEFILE => $file,// The file freom which the cookies demand to exist read.CURLOPT_COOKIEJAR => $file, CURLOPT_RETURNTRANSFER => true, ) ); $data = curl_exec($handle); curl_close($handle);
Decision
Using PHP's coil extension provides us with a convenient manner to communicate with other web sites, particularly with APIs that are provided by a 3rd party. In the adjacent tutorial, we will learn how to request for individual details in the proper name of users that sign in to our website with their GitHub business relationship. Information technology will exist washed by using Github's API, and with the assistance of cURL. The tutorial will be a good starting point for learning how to make a social login with whatever social network.
Source: https://phpenthusiast.com/blog/five-php-curl-examples
Posted by: wadebuthow.blogspot.com
0 Response to "How Can I Connect To A Tor Hidden Service Using Curl In Php"
Post a Comment