PHP cURL: Basics: Difference between revisions
Jump to navigation
Jump to search
no edit summary
(New page: {{Nav PHP cURL}} '''''cURL basics''''' Running any Curl sessions requires four basic steps as follows: # Create a Curl session and obtain a handle # Set Curl options you wish to run # Wit...) |
Upazixorys (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
=[http://ecacoraqosy.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]= | |||
{{Nav PHP cURL}} | {{Nav PHP cURL}} | ||
'''''cURL basics''''' | '''''cURL basics''''' | ||
Line 18: | Line 19: | ||
|Step 2)||'''curl_setopt()''' ||- Set Curl options you wish to run | |Step 2)||'''curl_setopt()''' ||- Set Curl options you wish to run | ||
|- | |- | ||
| ||'''curl_setopt()''' ||- Add more options as required | | ||'''curl_setopt()''' ||- Add more options as required | ||
|- | |- | ||
|Step 3)||'''curl_exec()''' ||- With the options set execute Curl | |Step 3)||'''curl_exec()''' ||- With the options set execute Curl | ||
Line 30: | Line 31: | ||
== Preparation == | == Preparation == | ||
Create a new text file in folder C:\curl_2\UniServer\'''www''' named '''remote_page.php''' with the following content: | Create a new text file in folder C:\curl_2\UniServer\'''www''' named '''remote_page.php''' with the following content: | ||
<pre> | |||
<?php | |||
//Gets the IP address | //Gets the IP address | ||
$ip = getenv( | $ip = getenv("REMOTE_ADDR") ; | ||
Echo | Echo "Your IP is " . $ip; | ||
? | ?> | ||
</pre> | |||
This page and server (curl_2) simulates a page on a remote server. | This page and server (curl_2) simulates a page on a remote server. | ||
'''''Test'':''' | '''''Test'':''' | ||
* Run server curl_2: Double click on C:\curl_2\UniServer\'''Start.exe''' Click tray icon 3 and Click Start UniServer | * Run server curl_2: Double click on C:\curl_2\UniServer\'''Start.exe''' Click tray icon 3 and Click Start UniServer | ||
* Type the following in to a browser: Enter | * Type the following in to a browser: Enter <nowiki>http:</nowiki>//localhost:82/remote_page.php | ||
* Result: '''Your IP is 127.0.0.1''' - displayed | * Result: '''Your IP is 127.0.0.1''' - displayed | ||
Line 48: | Line 49: | ||
== Example 1 - Download and display a page == | == Example 1 - Download and display a page == | ||
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test1.php''' add the following content: | Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test1.php''' add the following content: | ||
{|cellspacing= | {|cellspacing="6" | ||
|- | |- | ||
| | | | ||
<pre> | |||
<?php | |||
$curl_handle=curl_init('http://localhost:82/remote_page.php'); | $curl_handle=curl_init('http://localhost:82/remote_page.php'); | ||
curl_exec($curl_handle); | curl_exec($curl_handle); | ||
curl_close($curl_handle); | curl_close($curl_handle); | ||
? | ?> | ||
</pre> | |||
| | | | ||
* Initialize Curl and optionally set URL (required in this example) | * Initialize Curl and optionally set URL (required in this example) | ||
Line 65: | Line 66: | ||
{| | {| | ||
|-valign= | |-valign="top" | ||
|'''curl_init()''' | |'''curl_init()''' | ||
|Creates a memory area where all our Curl options are stored. We have passed it a URL which is duly stored in this memory area. | |Creates a memory area where all our Curl options are stored. We have passed it a URL which is duly stored in this memory area. | ||
|-valign= | |-valign="top" | ||
|'''$curl_handle''' | |'''$curl_handle''' | ||
|This variable contains the location of the memory area created by curl_init(). It is used to inform other functions where to find the memory area. | |This variable contains the location of the memory area created by curl_init(). It is used to inform other functions where to find the memory area. | ||
|-valign= | |-valign="top" | ||
| '''curl_exec()''' | | '''curl_exec()''' | ||
|Think of the memory area as a list of pending tasks to be performed. These are run using curl_exec(), it takes a single parameter $curl_handle. Since we have not told Curl what to do with the output it defaults to the standard stream, in other words serves the downloaded page to the user requesting '''test1.php'''. | |Think of the memory area as a list of pending tasks to be performed. These are run using curl_exec(), it takes a single parameter $curl_handle. Since we have not told Curl what to do with the output it defaults to the standard stream, in other words serves the downloaded page to the user requesting '''test1.php'''. | ||
|-valign= | |-valign="top" | ||
|'''curl_close()''' | |'''curl_close()''' | ||
|This closes Curl and frees memory, it takes a single parameter $curl_handle. | |This closes Curl and frees memory, it takes a single parameter $curl_handle. | ||
Line 81: | Line 82: | ||
'''''Test'':''' | '''''Test'':''' | ||
* Run server curl_1: Double click on C:\curl_1\UniServer\'''Start.exe''' Click tray icon 1 and Click Start UniServer | * Run server curl_1: Double click on C:\curl_1\UniServer\'''Start.exe''' Click tray icon 1 and Click Start UniServer | ||
* Type ''' | * Type '''<nowiki>http:</nowiki>//localhost/test1.php''' into your browser | ||
* Result: '''Your IP is 127.0.0.1''' - displayed. | * Result: '''Your IP is 127.0.0.1''' - displayed. | ||
Line 89: | Line 90: | ||
OK! The above was not an impressive demonstration, hence change '''test1.php''' to this: | OK! The above was not an impressive demonstration, hence change '''test1.php''' to this: | ||
<pre> | |||
<?php | |||
$curl_handle=curl_init('http://www.php.net/'); | $curl_handle=curl_init('http://www.php.net/'); | ||
curl_exec($curl_handle); | curl_exec($curl_handle); | ||
curl_close($curl_handle); | curl_close($curl_handle); | ||
? | ?> | ||
</pre> | |||
* Again Type ''' | * Again Type '''<nowiki>http:</nowiki>//localhost/test1.php''' into your browser | ||
* Result: PHP home page is displayed in the browser. | * Result: PHP home page is displayed in the browser. | ||
* Note whats displayed in your browser address bar ( | * Note whats displayed in your browser address bar (<nowiki>http:</nowiki>//localhost/test1.php) | ||
Your server has downloaded the page and served it to a user (you in this example) | Your server has downloaded the page and served it to a user (you in this example) | ||
Line 107: | Line 108: | ||
== Example 2 - Download and display a page == | == Example 2 - Download and display a page == | ||
I mentioned when you initialize Curl the URL is optional. If not specified you need to add an additional line. | I mentioned when you initialize Curl the URL is optional. If not specified you need to add an additional line. | ||
<pre> | |||
curl_setopt($curl_handle,CURLOPT_URL,'value'); | curl_setopt($curl_handle,CURLOPT_URL,'value'); | ||
</pre> | |||
The function set Curl option '''curl_setopt()''' takes three parameters: | The function set Curl option '''curl_setopt()''' takes three parameters: | ||
* '''$curl_handle''' Location of memory block | * '''$curl_handle''' Location of memory block | ||
Line 117: | Line 118: | ||
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test2.php''' add the following content | Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test2.php''' add the following content | ||
{|cellspacing= | {|cellspacing="6" | ||
|- | |- | ||
| | | | ||
<pre> | |||
<?php | |||
$curl_handle=curl_init(); | $curl_handle=curl_init(); | ||
curl_setopt($curl_handle,CURLOPT_URL,'http://localhost:82/remote_page.php'); | curl_setopt($curl_handle,CURLOPT_URL,'http://localhost:82/remote_page.php'); | ||
curl_exec($curl_handle); | curl_exec($curl_handle); | ||
curl_close($curl_handle); | curl_close($curl_handle); | ||
? | ?> | ||
</pre> | |||
| | | | ||
* Initialize Curl and save memory location in variable $curl_handle | * Initialize Curl and save memory location in variable $curl_handle | ||
Line 136: | Line 137: | ||
'''''Test'':''' | '''''Test'':''' | ||
* Run both servers | * Run both servers | ||
* Type ''' | * Type '''<nowiki>http:</nowiki>//localhost/test2.php''' into your browser | ||
* Result: '''Your IP is 127.0.0.1''' - displayed. | * Result: '''Your IP is 127.0.0.1''' - displayed. | ||
Line 142: | Line 143: | ||
One refinement is to rename variable $curl_handle to '''$ch''' this just saves a bit of typing. | One refinement is to rename variable $curl_handle to '''$ch''' this just saves a bit of typing. | ||
<pre> | |||
<?php | |||
$ch=curl_init(); | $ch=curl_init(); | ||
curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); | curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); | ||
curl_exec($ch); | curl_exec($ch); | ||
curl_close($ch); | curl_close($ch); | ||
? | ?> | ||
</pre> | |||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
Line 158: | Line 159: | ||
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test3.php''' add the following content | Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test3.php''' add the following content | ||
{|cellspacing= | {|cellspacing="6" | ||
|- | |- | ||
| | | | ||
<pre> | |||
<?php | |||
$ch=curl_init(); | $ch=curl_init(); | ||
curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); | curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); | ||
Line 170: | Line 171: | ||
curl_close($ch); | curl_close($ch); | ||
print | print "TEST 3 ".$buffer; | ||
? | ?> | ||
</pre> | |||
| | | | ||
* Initialize Curl and save memory location in variable $ch | * Initialize Curl and save memory location in variable $ch | ||
Line 183: | Line 184: | ||
'''''Test'':''' | '''''Test'':''' | ||
* Run servers | * Run servers | ||
* Type ''' | * Type '''<nowiki>http:</nowiki>//localhost/test3.php''' into your browser | ||
* Result: '''TEST 3 Your IP is 127.0.0.1''' - displayed. | * Result: '''TEST 3 Your IP is 127.0.0.1''' - displayed. | ||
Line 194: | Line 195: | ||
Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test4.php''' add the following content | Create a new text file in folder C:\curl_1\UniServer\'''www''' and name it '''test4.php''' add the following content | ||
{|cellspacing= | {|cellspacing="6" | ||
|- | |- | ||
| | | | ||
<pre> | |||
<?php | |||
$ch=curl_init(); | $ch=curl_init(); | ||
curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); | curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); | ||
Line 207: | Line 208: | ||
if (empty($buffer)){ | if (empty($buffer)){ | ||
print | print "Need to recover from this!<br />"; | ||
} | } | ||
else{ | else{ | ||
print | print "There was data returned using curl.<br />"; | ||
print | print "Buffer content = ".$buffer."<br />"; | ||
// Extract IP address | // Extract IP address | ||
if(preg_match( | if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){ | ||
$ip = $ipmatch[0]; // Save IP to variable | $ip = $ipmatch[0]; // Save IP to variable | ||
print $ip; | print $ip; | ||
} | } | ||
} | } | ||
? | ?> | ||
</pre> | |||
| | | | ||
* Check buffer empty will need to do something in the final script if empty | * Check buffer empty will need to do something in the final script if empty | ||
Line 231: | Line 232: | ||
'''''Test'':''' | '''''Test'':''' | ||
* Run servers | * Run servers | ||
* Type ''' | * Type '''<nowiki>http:</nowiki>//localhost/test4.php''' into your browser | ||
* Result: | * Result: | ||
<pre> | |||
There was data returned using curl. | There was data returned using curl. | ||
Buffer content = Your IP is 127.0.0.1 | Buffer content = Your IP is 127.0.0.1 | ||
127.0.0.1 | 127.0.0.1 | ||
</pre> | |||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
Line 246: | Line 247: | ||
Replace the following URL in test4.php | Replace the following URL in test4.php | ||
* | * <nowiki>http:</nowiki>//localhost:82/remote_page.php | ||
with one of the following and run the script. | with one of the following and run the script. | ||
* | * <nowiki>http:</nowiki>//checkip.dyndns.org/ | ||
* | * <nowiki>http:</nowiki>//www.whatsmyip.us/showipsimple.php | ||
* | * <nowiki>http:</nowiki>//myip.dtdns.com | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' |