Tải bản đầy đủ (.pdf) (71 trang)

curl php book v1.8 tiếng anh cơ bản

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (459.15 KB, 71 trang )
































































 !"#$"
%&'  (((
 )*(((


PHP/CURL Book with Examples


+,(
  

This Book is a collective work consisting of the following major Open Source companies: cURL, PHP,
OpenSSL, livehttpheaders, ethereal each licensed under a separate Open Source License.
is not affiliated with nor endorsed by any of the above providers. See
Copyrights for details

Version 1.1, June, 2005,Version 1.5, Jan, 2006,Version 1.7, Mar, 2006,
Version 1.8, Mar, 2006. This document is subject to change without notice.

+,(

PHP/CURL Book Copyright 2005-2006 and Imran - All Rights Reserved.
THIS COPYRIGHT INFORMATION MUST REMAIN INTACT AND MAY NOT BE MODIFIED IN ANY
WAY.
By purchasing this Book you agreed to accept the terms of this Agreement. This Agreement is a legal
contract, which specifies the terms of the license and warranty limitation between you and
. You should carefully read the following terms and conditions before using
this Book. Unless you have a different license agreement obtained from ,
installation or use of this Book indicates your acceptance of the license and warranty limitation terms
contained in this Agreement. If you do not agree to the terms of this Agreement, promptly delete and

destroy all copies of the Book.

This Book and its Examples are protected by copyright law. Unauthorized reproduction or distribution
of this Book and its Examples, or any portion of it, may result in severe civil and criminal penalties,
and will be prosecuted to the maximum extent possible under the law.

License to Use: You are allowed to use only one licensed copy of Book and its Examples on one web
site. You are not allowed to distribute the Book and its Examples and/or documentation by any
means without prior written permission from . All rights to the Book and its
Examples and documentation not expressly granted under this Agreement are reserved to
.

Disclaimer of Warranty:
THIS Book IS PROVIDED "AS IS", AND, ANY EXPRESS OR IMPLIED WARRANTIES INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Book, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE. THE USER MUST ASSUME THE ENTIRE RISK OF
USING THIS PROGRAM. IN NO CASE WILL Webradev.com OR LIABILITY EXCEED THE
AMOUNT OF THE LICENSE FEE ACTUALLY PAID BY LICENSEE TO ' cURL PHP Book'. IF ANY
PROVISION OF THIS AGREEMENT IS FOUND TO BE LEGALLY UNENFORCEABLE, THEN
THAT PROVISION WILL BE DELETED FROM THIS AGREEMENT, AND THE REMAINDER OF
THE AGREEMENT WILL CONTINUE IN FULL FORCE AND EFFECT.
PHP/CURL Book with Examples



  



1.0 Welcome to PHP/CURL Book ………………………………….……… 05

1.1 About this Book………………………………………………… 06
1.2 About the Author………………………………………………… 07

2.0 Getting Started with CURL PHP Book ………… …………………… 08

2.1 Before You Start……………………………………….………… 08
2.2 Technical Requirements………………………………………… 08
2.3 Installation………………………………………………………… 08

3.0 Curl Library ………………………………………… …………………… 10

3.1 What is cURL ?………………………….………… 10
3.2 What is libcurl ?………………………….………… 11
3.3 What is PHP/CURL ?………………………….…12
3.4 Who write PHP/CURL functions?…………….…12
3.5 The HTTP Protocol ……… …………………… 12
3.6 To cURL or to libcurl?……… ………………… 12

4.0 PHP/CURL Functions with Examples …………………………………… 13

4.1. Using libcurl with PHP…………………………………………… 13
4.2. Simple usage……………………………………………………… 13
4.3. Forms …………………………………….……………….…………15

1.1 GET …………………………………… ………….……………… 15
1.2 POST ……………………………………………………………… 16
1.3 Hidden Fields ………………………………………… ………… 17
1.4 Figure Out What A POST Looks Like ….……………………… 18
4.4. Download Image (Binnary File) …………………… …………… 18
4.5 Authentication ……………………………………………………… 19
4.6. Referer ……………………………………………………………… 19
4.7. User Agent …………………………………………….…………… 20
4.8. Redirects …………………………………………………………… 21
4.9. Cookies ……………………………………………… …………… 22
4.10. HTTPS (SSL) ……………………………………………………… 25
4.11. Debug ……………………………………………… …………… 26
4.12. XML, Credit Card Processing Solutions……………………… 28
4.13. Post Custom Header to the Remote (Target) Server ……… 30
4.14. Upload a file to remote server from HTML form …… … 32
4.15. FTP Upload a file ………………………………………… 32
4.16. FTp Delete a File ……………………………………… … 33







PHP/CURL Book with Examples


  
5.0 LibCURL Options ……………………………………… ……………… 35


6.0 Header Tracking Tools ……………………………….………………… 42

7.0 RealTime Example ……………………………………….…………… 44
7.1. Ebay CURL PHP Login Script ………………… …………………… 44
7.2. Ebay Summary Page After Login with PHP… ………….………… 48
7.3 Calculate Fedex Shipping Charges with XML CURL PHP support. 51
7.4 Yahoo Email login and download Address Book . ………… 55
7.5 PayPal Login and download Transaction History. ………… 55
7.6 Hotmail Login and download Contacts List. ………… 55
7.7 Gmail address book download . ………… 55

8.0 Regular Expressions ……………………………………….…………… 56
8.1. What are Regular Expressions? ……………… …………………… 56
8.2. What exactly is possible with Regular Expressions?…….………… 56
8.3 Regular Expressions in PHP…………………………………………… 56
8.4 Regular Expressions Examples ……………………………. ………… 59
8.4.1 preg_match function ………… ………………………. ………… 59
8.4.2 preg_match_all function ………………………………… ………… 60
8.4.3 preg_replace function ………………………………… ………… 62
8.4.4 preg_replace function ………………………………… ………… 62
8.4.5 'ereg','eregi','ereg_replace'…………………………… ………… 63
8.4.6 Parsing Form for hidden fields ………………………… ………… 64
8.4.7 Parsing Images linkes from HTML page.……………… ………… 65


Appendix A …………………………………………………………………… 67

Appendix B …………………………………………………………………… 70

References …………………………………………………………………… 71

PHP/CURL Book with Examples


  



                    
!   "# $ %  &   &   % # '  & 
(  & &  #  )    ! * +     &  &   &     ,&    &  
-  )   & # #  ".  -  ".   & ! &  "     "
' & &  -        "$  &  -  !  " )   &    
&     /    )  #  & &  &   "-  &  -  "0    
 & -   )  -   "   & & .   & !  !  "  -  -   & - 
 

       & -  ! & -  # $ %  "!   "# 
$ %  &   &  & -  )   & 1    &    )  
 # #    -   & - # ! /    &   2  &  0   "
 "  )   0   & ! &  -  & )    # /    &  # )   & ! 
 #      


(    +
(    +(    +
(    +



      % $ 

2.  # ) .  !  -  - 
3  1  & .  -  
4 -   & ! .  &  
5 -  -   & ! .  &  
6 7 8   & ! .  &  
9  #  ) ! & .  
: & &  #     
;     ( -   . .   & !  !  
0    ! &  & - -  &  - / - -   
   ! &  & - -  &  - &    
2 )  ! &  & - -  &  -   &   &  ) 












PHP/CURL Book with Examples


  
/ #   
/ #   / #   
/ #   




)  #     &   & &     ! # )         & - 
   &    /    &  - %  - &   #     
# ! & &   - &  "    &      & -  - %  & %  !  
  /    &  # )   & !      

  & #  )         & !  # #   & -   -  
  & - #          # # - %      
& -   &  #   &  & !     ) &   )   &   ) ! 
   & !  & )    & !   +(  "(  . "  "  . "' 1  "
  <   "$ = "( =  & - $ /           .    "  
1 .  "   "(     - & ! "#  "   #   -    - "   "
 "  *#   ,       -   &  &  - &    
  &  $ %   # ) - %  & ! #   -     &  & 
"# )  & ! - %     &  (  

 &   & !   &      &  % & !  #    - >  
.  & ! > ?  & ) ?   -  & -   & -  #  #     ) 
  &  &   # "     "     - -    # 
 %      &    -  ) 

     & & - -   !    & -  # - %   &  - & 
  & !  #  %  &     &    %  0   
& -  ! &  &  - !   & !   ! 

0   &   )   # #  "  & ! &  "  -   & -  &      
 - & !  # &  # #     8    & - <    "  # # "
  - * ! & , &     )    <  & -  -    %  & " & - 

  & -    &   & ?  #   &   &  

.  -   & -  ! &   &       &  )   &  #      "
&  - & ! +-  &  - & ! &  & & $ /  " & !  # @#   -   " & - 
 & !    &    &  &    - & =& & .  -   & - 
 ! &   &   & &  # & & @ - &   # &      !  "   
 &   & - %  ) &  A  .  -   & -  #  ! &   & ) - # ) 
 ! &  &  - %   

          -   #
          -   #          -   #
          -   # @
@@
@#   & - +
#   & - +#   & - +
#   & - +



B.   =& &  &         &     ) "
B%  @ -  "
B8 &      & &   &   & -     )  >  @#   - ># -  "
B  )  & &   & &    )    "
B&  &  & &     &  " & - 
B  & & & - !  >  & !  % >& )    ")   &  
PHP/CURL Book with Examples


  
2/ #  /  

2/ #  /  2/ #  /  
2/ #  /  



8    - = & C  - 
D 1 $ & ) " A  #  - "'  E &    "   & 
& +F ; 25 5 6 6 2; 5 5 "+F ; 2345 6 ; 444
# + + &    "  + & G    

8    - = & C  - =& & - %  &   &   - & 2<   
-  )  " %    %    & - -      & & 
# $ %  &  & -  & &  &    & )  #   " % - & ! !   
  -     & !  #   ". . "7  8 .   & ! &  1  A  & 
 & ?   "#  -  #   3  & -  -   & - 7 8 ' !       "
    @ @@  @  "1 & & .   & ! .  "# # "
.  -   & - #  ! &  & !  @@     - ) &  -   @- % &  # 
    &  & / 8 *&   F /   F 8 ) . H F ," & - 7 8 
#   -  #  %     )  &  ! & ! 8 0 . H $   #    %  "==. 
# . %  

=     -  & - - %  - =& &  E   &  &  - & ! 
 #  ) "=8  "  & -  -     ! &      1 !  & A  
& !  &     &  & -   & ! -  &  - +1 1 C & 
G . / /   -  25 &    & - $ %  & "' .    & - # 
 & ?   /  =& & )   "   & & &   (  
 +    #   # )  & - =& &    # .  -  
  - &   & -    &  $ %   

8  & ) "   -  &    - %  &   E    

)   - %  &     )    &   & )  & !    - 
   &  & !    &  ".  & ! &  "-  &  - & !  # 
 !  
PHP/CURL Book with Examples

2' & ! .  -  

2 0  .  
2 0  .  2 0  .  
2 0  .  



  !  -       )   %   - ) &   -  # . % *==. "/ /  ,
 # . % *==. "/ /  , # . % *==. "/ /  ,
 # . % *==. "/ /  ,
 & -  & & & ! 
 & -  & & & !  & -  & & & ! 
 & -  & & & !      
          
     &  & - )          
= )   &    )   )   ?  &     
?  -  " )   &  !  )   - &   

We also explain in details how to install # . % *==. "/ /  , & -  & & & ! 
# . % *==. "/ /  , & -  & & & ! # . % *==. "/ /  , & -  & & & ! 
# . % *==. "/ /  , & -  & & & ! 


     

          
      software in Appendix A, Appendix B.

22 &  ?  & 
22 &  ?  & 22 &  ?  & 
22 &  ?  &  



 )  # ! &   & !  )  "&    )   %          
&   -  & - & !  - & )   )  / )   & -  I

=& -     & &  )  & - &      ! 
* +     ,?    )    9 2@#  !  
&    & ) %  &  #  %  & 9 2@#  =& 423")   
& - %  & 9 ; ! ( 43")   & -  %  & 
 J 9 ; : ! 5   ) ?   %  & !   & 
9 5 

23=&    &
23=&    &23=&    &
23=&    & 

   J     )       @@ @ KL $ =M 
$ =  &  - ) &  & & ! #  & - &  - -  =& 
>&  - >- )   - #   - &  - > >   - &  & 
  )  & -       - #   &  - #    - & ># >
- ) ! & & & !  43)   & & !     
   @@ @      
























PHP/CURL Book with Examples


 
< & 32  +
< & 32  +< & 32  +
< & 32  +=& - &  #  -  &  & -   & % & & "
)     ) #  ) 32-  & -    ) 32-  $  -  & 32

# &  )    ! . 0 .   8  -  )  & -    & *  +
+N=< <  N. 0 .   8 32+N=< $ 1 . N. 0 .   8 ,     & )  O  & P
*  ++N=< <  N. 0 .   8 32+N=< $ 1 . N. 0 .   8 , & - 
 & ! & OQ &  & L   R  - P
  O &  & L   R  - P
*% & )    & ,

( -   & =&    &    %  /   & -  / "
/   & -  / "/   & -  / "
/   & -  / "

/   & -  
/   & -  /   & -  
/   & -  












































PHP/CURL Book with Examples



 
3#  ) 

 #    % -  & &      &    & -    #  % 
 )  %        - %  )    *  . ,! % & !  & - ! 
% #  @&   & &     & *, #     - @  "=% 6 
  # " & -     & ) & ! )   #  &     
)   #  - & !        #   ! "     ) & !    ) "
%  ! & !   & &  #  &  &   %   

 & - #   #        #  %  &      
    & !  %  )  =& &    #    ! ) & !   # "
  & !    )  & ) )   & @ % ?   #   - )   & !  
 "  #  %  &     & " & !  & -   & - & ! ?      & ! 
 & ) & ! ) #  &   "7 8 @". 1 / " / C 

 #  )  &  & - % ) & !  )  =  ?    "!  
-   " & -  -    & - %  &  & 0   #  # ) & - !  
% ) & ! !   & !  & -     & !   !   -  &   & %  


3
33
3

   S
  S  S
  S 




 &      ) & J&   J"! &  )    - &      
 # %   -         &   #  &  & - J J  
 - "     &  # # %  &  >& ?   #  ) > ) & 
  % %  & +> ?   #  ) >  E -    
 -   #   & -  

#  
#  #  
#  



/   & -   ) @@  & @ -  &  #  ) "    & ! (  " . "  "
  . "' 1  "  <   "$ = "( =  & - $ / #         . 
   "  1 .  "   "(     - & ! "#  "  #   - 
   - "   " "  F      -   &  & "  &    " 
  )  & & & !  & - T

#   ! )   # "#  -   & -   - &  ) & &       "
&  - & ! .   "< . $ "( . $ "1  & . $ "$   & "7 "=7 "/ =7 "  6 4"
&   "&   "$ "& -   "/ !  "1 . 2"1  "8  . 7 " "H < 7 "
1  & D 8 . "=. 1 . "< % <  "$ 1 .  & - 

#    " - @  "=% 6   # "   "    -  & -    

PHP/CURL Book with Examples


 

 
  
 



/  & - &  ! & !  & - & !     & !  ) &   

. &     #  "       & !  & =& &   " & ) 
&  - & !   "  . "(  "(  . "' 1  "$ / "$ = "  <    & - ( = 

 &  &   & -   & &    & - +K M

  &      # @ E  & -  -  E       -  & 
 E&   & %    # &  &  "#  )    -  &    
- -   & - @& &  -  * & - #  #  ) ," & -  ) 
 & # %  -   @ -  E 
32  #  S
32  #  S32  #  S
32  #  S 



#     #  & -   # #  )   % -  )    &   ) &   
  & !  & =& &   0   &   #    & )  
    & "#  &   "  - @  #     #  # ) 
   # "      & -   &   - @#   -  @    
 &  #  ) &    & @#  &    

  & -   >&  >" & -    - %  - # ) $  & . & # ! & ; ; :    

 & - & #      # #  )   % -   &   ) &   
  & &  ) =  -   "=% 6   # " & -         & 
& & &   #  # & - & !     - - - # ) . & !  !  

 & - #   &  &      & !   - %  )    "&  - & ! 
  "  . "(  "(  . "' 1  "$ / "$ = "  <    & - ( =  
#    & &    )  & ) B< =7   & !  )  "     & -   "1 . 2"
1 . " & -  & ) 

 #     )  &   "  & 8 = 7 - %  % &    
&   % ) #  "  & !       % )    & " 
& 0   &   #    " & - % & &  -  & - -  #   )   & 
    & "   - @  

  - & # &   -     & "   
 -   &  -  !  & !  & !   ! " 







PHP/CURL Book with Examples


 
33
3333
33


  S
  S  S
  S 


 -         #   !       @
 & &    & 

=&  E   -  -  &    
 & - &  & - #  #  )    % -  &  
 *  & &  &   &  , )    & * &   & !     , 
     &   &     &  & ! %     ! ?   &  - 

34
3434
34

 (  & &  S
 (  & &  S (  & &  S
 (  & &  S 



  -     !      ! =-  & -   & - 
   #  " )  & -  % #  &   -   )   #  
 & #   -   & # ) . & !  !  

35    
35    35    

35    



      -  -     #  %  =  % )   
   #    &  =       &  & !  & 
 %  &   & !    -  & -  "   #   & 

     & / . == &  # & !  & # ) &   % ?    
     & " & - &  %       &  #     
?   - & &   & & 

36  # 
36  # 36  # 
36  #  S
SS
S 


 -  &      #  -  & -  &    & ( 
&   & " = %  & E#  & & & !  @   &    & !  &  
 % " ) =. -  & U % #      & &   "  & ! 
   &   % " = % #      &  & - = 
#  - & !      & ?  & !   & &  ) "#   ! 














PHP/CURL Book with Examples


 
4(  & &   
     

=      &  # - & "  & *,  & &  -    ) &     
  #  0   ?   - #   & ! )       !  
& 
V S   

  & *,Q

S W 
 
  
 


    
        
    


 &  # - 
=&  & 
=&  & =&  & 
=&  & 

 #  9 21  & . . ; 9 A # 4

4 & ! # 
4 & ! # 4 & ! # 
4 & ! #   
    
  



  & !   &   &  & "  & ! #  # & - & !  
   "   )  & !   &   1 .    & 

       & ! #    &       & !  #   
   +
=&  A    & .  &  * -   &   & 
  & ,    &  &    &      & 

42.    ! 
42.    ! 42.    ! 
42.    ! 




     & -  & ?     &  -   & !    !  
  -      #   ! " &  !    & 
     '   ?    %  & - %  -  &   -  


























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


 



43(  
43(  43(  
43(  



(   ! &    )   #   &   &   8   !   -   
  & -   & " & - &     & -  J1 C JJ  # 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 3Y    L 1 C >   & - -      
 %   

= ! &       & &   ! >   )   & # >"
 & -   ! )  J!  # 
>   )   &     S &  L  & Y  ! L 3Y    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.

?>

21 . 
21 . 21 . 
21 .  



 '   -    &    - &   ! -    ) - &  -  
)  #     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 3Y    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@  '           - 

44$  &  - = ! *& &  ) ( ,
44$  &  - = ! *& &  ) ( ,44$  &  - = ! *& &  ) ( ,
44$  &  - = ! *& &  ) ( ,



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



 
45 /  &  &
45 /  &  &45 /  &  &
45 /  &  & 



/  &  &   # )  % )    &   & -      - 
   & %  )  )  J  - - ?   )  J- & !  
    &  &   - &   *  )      # ) -    , 
B  & BB 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.

?>


46  
46  46  
46  



/   ?    ) &  -  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.

?>

49  / ! & 
49  / ! & 49  / ! & 
49  / ! & 




D )     - "   ?     )   @/ ! & 
 - =&        ! & *& ,  # & !   - 8  & ) 
    &     &  & - -  -    )   !  . )  # 
 !   )  -  &   !       -  & #    
 #     #      #     )     ) 
  - -  & & -   E %    "% #   


/  ")     ! & !    !    &  &   
  !  )    & ! & !   !  )  #    & )  &  
   / ! &  -   % & & & ! )  J
&   #    






















PHP/CURL Book with Examples


 
<?php
// Example 006
// Pass User Agent to the Target Site. This insure that request is from
which Browser and Operating System.
// Copyright

$url = " // URL to POST Login Data.
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; //Agent
Setting for Internet Explorer
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4)
Gecko/20030624 Netscape/7.1 (ax)"; //Agent Setting for Netscape

$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_USERAGENT, $agent); // Agent 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.


?>

4: - 
4: - 4: - 
4: -  



&     ?   -    % " )   %  ) 
&  -  &  #   #     - ! &   & -    ! " 
&    !  & ! &  ) ! &  -      -   #   
-   & +

 -  &    & + -  # ) -    "#     ) -    ) 
    !  &    & & -    )      =-   % 
    &  &         & + &  

 #      & +

= )     1 .     -  ) -  )   & 
  ! ")   &   )   @ & - @- @( !   & )   1 .  & 
  ?   " & - & % '   &   & !   &  













PHP/CURL Book with Examples


 
<?php
// Example 007
// Redirect Page where Sever transfer control after login varifaction etc.
// if this option is not provided then this will not go to welcome page.
// Copyright

$url = " // URL

$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Redirect to page where its
goes after login.
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.

?>


4; 
4; 4; 
4;  



   )  # #    - >&  -   & > # )   & ! 
   E  &        - & &     
 & & # )  %   %  &      
 & -  &    &   & #  " & -    & -   &    & 
-   & -       

&  &  &      %   &   & -      %   ) 
   - &  % - "&  & -  #    & - 
& &   % " &      )    - 

8  & )     &   & -  %     - & &     ?    
&   & ! !     &  #  #    &     &  " 
  #  # -  & -  & - #     )  #     & 
        ) #    -   

   % ) =  &  &  )  .    ! & %   & ! 
  !  &    &    #    & !  ?  -  & 











PHP/CURL Book with Examples


 
@ 0   % (  
$cookie_file_path = "C:/Inetpub/wwwroot/spiders/cookie/cook"; //
Please set your Cookie File path. This file must have CHMOD 777
(Full Read / Write Option).

2@   (  & &  
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); // The name
of the file containing the cookie data. The cookie file can be in
Netscape format, or just plain HTTP-style headers dumped into a
file.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // The name
of a file to save all internal cookies to when the connection
closes.



  -   +
  -   +  -   +
  -   +@
@@
@






 - &       & -    - & )   % 
V S   

 &    *J   J,Q

S W 

2
22
2/     & )  #   )  -  &     & -  ) 
       - # )   %   (     
>+=&   #    ) -  &  -    >

3
33
3<       & -   %     & -    - & )  
 % 

4
44
4&    & )    & -  8 1 $ *9 9 9 ,  &  
* - " ,' %      &    )      - #  #  & 
1 &  & -   1 . "   ! ! & )   &  %  & - ! %      
% ) & 

5 
5 5 

5 <     & !  )   %       # )  - - & ! 
 & 2   & 
(     
>+=&   #    ) -  &  -  >













PHP/CURL Book with Examples


 





<?php
// Example 008
// Use Cookies for Storing, Reading, setting and Passing
// Copyright


$url = " // URL
$POSTFIELDS = 'name=admin&password=guest&submit=save';
$reffer = "
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4)
Gecko/20030624 Netscape/7.1 (ax)";
$cookie_file_path = "C:/Inetpub/wwwroot/spiders/cookie/cook"; // Please
set your Cookie File path. This file must have CHMOD 777 (Full Read /
Write Option).

$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // The URL to fetch. You can also
set this when initializing a session with curl_init().
curl_setopt($ch, CURLOPT_USERAGENT, $agent); // The contents of the "User-
Agent: " header to be used in a HTTP request.
curl_setopt($ch, CURLOPT_POST, 1); //TRUE to do a regular HTTP POST. This
POST is the normal application/x-www-form-urlencoded kind, most commonly
used by HTML forms.
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); //The full data to post
in a HTTP "POST" operation.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // TRUE to return the
transfer as a string of the return value of curl_exec() instead of
outputting it out directly.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // TRUE to follow any
"Location: " header that the server sends as part of the HTTP header (note
this is recursive, PHP will follow as many "Location: " headers that it is
sent, unless CURLOPT_MAXREDIRS is set).
curl_setopt($ch, CURLOPT_REFERER, $reffer); //The contents of the
"Referer: " header to be used in a HTTP request.


curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); // The name of
the file containing the cookie data. The cookie file can be in Netscape
format, or just plain HTTP-style headers dumped into a file.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // The name of a
file to save all internal cookies to when the connection closes.

$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


 
4  . *. . ,
4  . *. . ,4  . *. . ,
4  . *. . ,



       )  -      &    # )    & 
  - & !      ! &  ) &  &     . "  % 

. . . . & )    -      &  & - % - % &   & - 
     -       ) &  &  % &  & 

. . * .     %  &    & -  -   - ,   
  -   - %  & -         & )  &   & - ) 
&      &   & )  -   ?   

      & )  -    &   )  %   # 1  & . . 
#   


<?php
// Example 009
// HTTPS (SSL Pages)
// Copyright

$url = "https://your_Secure_site.com/login.php"; // URL
$POSTFIELDS = 'name=admin&password=guest&submit=save';
$reffer = "https://your_Secure_site.com/index.php";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4)
Gecko/20030624 Netscape/7.1 (ax)";
$cookie_file_path = "C:/Inetpub/wwwroot/spiders/cookie/cook"; // Please
set your Cookie File path. This file must have CHMOD 777 (Full Read /
Write Option).

$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // The URL to fetch. You can also
set this when initializing a session with curl_init().
curl_setopt($ch, CURLOPT_USERAGENT, $agent); // The contents of the "User-
Agent: " header to be used in a HTTP request.

curl_setopt($ch, CURLOPT_POST, 1); //TRUE to do a regular HTTP POST. This
POST is the normal application/x-www-form-urlencoded kind, most commonly
used by HTML forms.
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); //The full data to post
in a HTTP "POST" operation.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // TRUE to return the
transfer as a string of the return value of curl_exec() instead of
outputting it out directly.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // TRUE to follow any
"Location: " header that the server sends as part of the HTTP header (note
this is recursive, PHP will follow as many "Location: " headers that it is
sent, unless CURLOPT_MAXREDIRS is set).
curl_setopt($ch, CURLOPT_REFERER, $reffer); //The contents of the
"Referer: " header to be used in a HTTP request.
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); // The name of
the file containing the cookie data. The cookie file can be in Netscape
format, or just plain HTTP-style headers dumped into a file.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // The name of a
file to save all internal cookies to when the connection closes.

×