PHP/CURL Book with Examples
4( & &
= & # - & " & *, & & - ) &
# 0 ? - # & ! ) !
&
V S
& *,Q
S W
& # -
=& &
=& & =& &
=& &
# 9 21 & . . ; 9 A # 4
4 & ! #
4 & ! # 4 & ! #
4 & ! #
& ! & & & " & ! # # & - & !
" ) & ! & 1 . &
& ! # & & ! #
+
=& A & . & * - & &
& , & & & &
42. !
42. ! 42. !
42. !
& - & ? & - & ! !
- # ! " & ! &
' ? % & - % - & -
PHP/CURL Book with Examples
R & *,
R & *, R & *,
R & *,
& A & & & - & & -
R *X ,
R *X , R *X ,
R *X ,
& & - # - ) & A &
& - & & = )
- & - & *! % & # ) ,
R *X " & "% ,
R *X " & "% , R *X " & "% ,
R *X " & "% ,
. & & & - & - # )
& & " & - % %
& ! % &
R *X "1 R < / < . ( ",
R *X "1 R < / < . ( ", R *X "1 R < / < . ( ",
R *X "1 R < / < . ( ",
& ! & & =
& & # & -
R *X "1 R "
R *X "1 R " R *X "1 R "
R *X "1 R "X ,
X ,X ,
X ,
) !
% # - - ) & ! & &
R *X ,
R *X , R *X ,
R *X ,
' # & - % # & !
R *X ,
R *X , R *X ,
R *X ,
" & - )
' & ! #
' & ! #' & ! #
' & ! # @
@@
@ % +
% + % +
% +
<?php
// Example 001
// Simple Get Webpage
// Copyright
$url = " // From URL to get webpage
contents.
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return Page contents.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free up system resources.
echo $result; // Print page contents.
?>
PHP/CURL Book with Examples
43(
43( 43(
43(
( ! & ) # & & 8 ! -
& - & " & - & & - J1 C JJ # J
# & ! - & % % & ) )
- - - - & ! & - -
& - # " - - & & # ! ) "- ) & -
- - & & ! & ! & @ % ) & !
- #
1 # & - ! & % & - %
- ) & - 0 & & E & % & & !
'
' '
'
/ ' @ - ' " - & 8 +
<form method="GET" action="post.php">
<input type=text name="first_name">
<input type=text name="age">
<input type=submit name=press value="OK">
</form>
=& ) % # " # & & - @
# & # - >1 C >= ) & & L & & - ! L 25 & - 1 C # & "
) # & & ! ) !
> S & L & Y ! L 3Y L 1 C > & - -
%
= ! & & & ! > ) & # >"
& - ! ) J! #
> ) & S & L & Y ! L 3Y L 1 C >
8 & ! & )
& & O
& -
& - & -
& -
P& - %
? ) & ! ( " - & & ") & -
Z 2 & & O
& -
& - & -
& -
P # E#
PHP/CURL Book with Examples
8 ( ' - #
8 ( ' - # 8 ( ' - #
8 ( ' - # @
@@
@ % ! +
% ! + % ! +
% ! +
2
2 2
2
<?php
// Example 002.1
// Pass Form Variables as method = GET
// Copyright
$domain = " // URL to POST FORM.
$post_fields = 'get_page.php?fuseaction=forum&name=imran&age=30&press=OK';
$url = $domain . $post_fields;
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return Page contents.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free up system resources.
echo $result; // Print page contents.
?>
21 .
21 . 21 .
21 .
' - & - & ! - ) - & -
) # J ! & ) ! - & ! & ) & # #
# ! ) ! % & - "# & # % - - % & !
) & - & & & & -
! & - & ! % ) & ! & - & - #
& 1 . - ) & & -
- - & - ) & J & ) &
- - -
- % ) % & +
<form method="GET" action="post.php">
<input type=text name="first_name">
<input type=text name="age">
<input type=submit name=press value="OK">
</form>
/ & - - - & # "
- - +
>& L & Y ! L 3Y L 1 C P
& - 1 . & & @ )
& @ @ @ & - - & - - ) - 1 . & -
PHP/CURL Book with Examples
- ) & - % 8 . - ) # ) & - - "
& - ) ( " ) & - & & "
) & - Z 2( & ! )
) ) - # % - & ! ) & - -
<?php
// Example 002.2
// Pass form Variables as method = POST
// Copyright
$url = " // URL to POST FORM.
(Action of Form)
// use PHP Fucntion url_encode() for post variable for application/x-www-
form-urlencoded
$post_fields = 'fuseaction=forum&name=imran%20khalid&age=30&press=OK'; //
form Fields.
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_POST, 1); // use this option to Post a form
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); // Pass form Fields.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return Page contents.
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free up system resources.
echo $result; // Print page contents.
?>
3- - & ( -
3- - & ( - 3- - & ( -
3- - & ( -
/ % ) & ) 8 # - & & &
# & ! - - - - & - - - & -
- ) - & ") & J- ) - & - ) ! -
& ! E -
/ & % # - "& - - & - & - &
# # & - +
<form method="POST" action="process.php">
<input type=text name="first_name">
<input type=hidden name="status" value="married">
<input type=submit name="press" value="OK">
</form>
") & J % & # -
- - & & ) J +
R & L - Y L 1 C Y L &
PHP/CURL Book with Examples
4( ! 1 / 1 .
4( ! 1 / 1 . 4( ! 1 / 1 .
4( ! 1 / 1 .
- & ! & ! 25 ) %
? & - & ' & - & ) & !
& ) J # & & - & - % # ) & ! & -
# ") J % ) & - & & - & ! 1 . )
) ) # -
/ & ) ) ! " % 8 ! &
) - "- ) J- J ' " & - # # &
*) - & ! & ) & ,
0 & ) - ! & - - " -
JS J@ ' -
44$ & - = ! *& & ) ( ,
44$ & - = ! *& & ) ( ,44$ & - = ! *& & ) ( ,
44$ & - = ! *& & ) ( ,
0 & - & - & ! # & ) & & ) - " - "- "A
' ! & ! & ? ! # !
<?php
// Example 003
// Download Image (Binnary File)
// Copyright
$url = " // URL to Download
Image
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return stream contents.
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // We'll be returning this
transfer, and the data is binary
$data = curl_exec($ch); // // Grab the jpg and save the contents in the
$data variable
curl_close($ch); // close curl resource, and free up system resources.
// Set the header to type image/jpeg, since that's what we're
// displaying
header("Content-type: image/jpeg");
echo $data; // Print stream contents.
?>
PHP/CURL Book with Examples
45 / & &
45 / & &45 / & &
45 / & &
/ & & # ) % ) & & - -
& % ) ) J - - ? ) J- & !
& & - & * ) # ) - ,
B & BB B# - " & & - & & - - & )
! ) # - "# ) - # # ) & ) & & &
& # & ) & - %
# & - & & & $ !
/ & & +
<?php
// Example 004
// Login to site where Dialog Box Open for Authentication
// Copyright
$url = " // URL to POST Login Data.
$post_fields = 'username:password'; // PopUp Dialog Login Fields.
// Do not remove the ":" sign between username and password.
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_ USERPWD, $post_fields); // Dialog Box
Authentication.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return Page contents.
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free up system resources.
echo $result; // Print page contents.
?>
46
46 46
46
/ ? ) & - J J - *) - ,"
& # - & !
. ! - ? % )
& J % & ! & & & & & & !
- ) & ! ) ! - " & )
- & ! ") & & ) & ! ) & & @ - & -
) # # % & % & ! ) ?
# - +
PHP/CURL Book with Examples
<?php
// Example 005
// Pass Refferal to the Target Site. This insure that request is from this
site.
// Copyright
$url = " // URL
$reffer = " // Refferal site
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_REFERER, $reffer); // Refferal site Setting.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return Page contents.
$result = curl_exec($ch); // grab URL and pass it to the variable.
curl_close($ch); // close curl resource, and free up system resources.
echo $result; // Print page contents.
?>
49 / ! &
49 / ! & 49 / ! &
49 / ! &
D ) - " ? ) @/ ! &
- =& ! & *& , # & ! - 8 & )
& & & - - - ) ! . ) #
! ) - & ! - & #
# # # ) )
- - & & - E % "% #
/ ") ! & ! ! & &
! ) & ! & ! ! ) # & ) &
/ ! & - % & & & ! ) J
& #