PHP cURL: Basics: Difference between revisions

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...)
 
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>
&lt;pre&gt;
<?php
&lt;?php
   //Gets the IP address
   //Gets the IP address
   $ip = getenv("REMOTE_ADDR") ;
   $ip = getenv(&quot;REMOTE_ADDR&quot;) ;
   Echo "Your IP is " . $ip;
   Echo &quot;Your IP is &quot; . $ip;
?>
?&gt;
</pre>
&lt;/pre&gt;
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 <nowiki>http:</nowiki>//localhost:82/remote_page.php
* Type the following in to a browser: Enter &lt;nowiki&gt;http:&lt;/nowiki&gt;//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="6"
{|cellspacing=&quot;6&quot;
|-
|-
|
|
<pre>
&lt;pre&gt;
<?php
&lt;?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);
?>
?&gt;
</pre>
&lt;/pre&gt;
|
|
* 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="top"
|-valign=&quot;top&quot;
|'''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="top"
|-valign=&quot;top&quot;
|'''$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="top"
|-valign=&quot;top&quot;
| '''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="top"
|-valign=&quot;top&quot;
|'''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 '''<nowiki>http:</nowiki>//localhost/test1.php''' into your browser
* Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//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>
&lt;pre&gt;
<?php
&lt;?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);
?>
?&gt;
</pre>
&lt;/pre&gt;
* Again Type '''<nowiki>http:</nowiki>//localhost/test1.php''' into your browser
* Again Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//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 (<nowiki>http:</nowiki>//localhost/test1.php)
* Note whats displayed in your browser address bar (&lt;nowiki&gt;http:&lt;/nowiki&gt;//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>
&lt;pre&gt;
curl_setopt($curl_handle,CURLOPT_URL,'value');   
curl_setopt($curl_handle,CURLOPT_URL,'value');   
</pre>
&lt;/pre&gt;
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="6"
{|cellspacing=&quot;6&quot;
|-
|-
|
|
<pre>
&lt;pre&gt;
<?php
&lt;?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);
?>
?&gt;
</pre>
&lt;/pre&gt;
|
|
* 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 '''<nowiki>http:</nowiki>//localhost/test2.php''' into your browser
* Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//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>
&lt;pre&gt;
<?php
&lt;?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);
?>
?&gt;
</pre>
&lt;/pre&gt;
'''''[[#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="6"
{|cellspacing=&quot;6&quot;
|-
|-
|
|
<pre>
&lt;pre&gt;
<?php
&lt;?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 "TEST 3 ".$buffer;
print &quot;TEST 3 &quot;.$buffer;
?>
?&gt;
</pre>
&lt;/pre&gt;
|
|
* 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 '''<nowiki>http:</nowiki>//localhost/test3.php''' into your browser
* Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//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="6"
{|cellspacing=&quot;6&quot;
|-
|-
|
|
<pre>
&lt;pre&gt;
<?php
&lt;?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 "Need to recover from this!<br />";
   print &quot;Need to recover from this!&lt;br /&gt;&quot;;
}
}


else{
else{
   print "There was data returned using curl.<br />";
   print &quot;There was data returned using curl.&lt;br /&gt;&quot;;
   print "Buffer content = ".$buffer."<br />";
   print &quot;Buffer content = &quot;.$buffer.&quot;&lt;br /&gt;&quot;;


   // Extract IP address  
   // Extract IP address  
  if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){   
  if(preg_match(&quot;/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/&quot;, $buffer, $ipmatch)){   
     $ip = $ipmatch[0]; // Save IP to variable
     $ip = $ipmatch[0]; // Save IP to variable
     print $ip;                                 
     print $ip;                                 
  }
  }
}
}
?>
?&gt;
</pre>
&lt;/pre&gt;
|
|
* 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 '''<nowiki>http:</nowiki>//localhost/test4.php''' into your browser
* Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//localhost/test4.php''' into your browser
* Result:
* Result:
<pre>
&lt;pre&gt;
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>
&lt;/pre&gt;


'''''[[#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
* &lt;nowiki&gt;http:&lt;/nowiki&gt;//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/
* &lt;nowiki&gt;http:&lt;/nowiki&gt;//checkip.dyndns.org/
* <nowiki>http:</nowiki>//www.whatsmyip.us/showipsimple.php
* &lt;nowiki&gt;http:&lt;/nowiki&gt;//www.whatsmyip.us/showipsimple.php
* <nowiki>http:</nowiki>//myip.dtdns.com
* &lt;nowiki&gt;http:&lt;/nowiki&gt;//myip.dtdns.com


'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
322

edits