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

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

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

            !"#$" %&'  (((  )*(((  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. http://curl.phptrack.com is not affiliated with nor endorsed by any of the above providers. See Copyrights for details http://curl.phptrack.com/copyrights/LICENSE_AGREEMENT.txt. 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 http://curl.phptrack.com 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 http://curl.phptrack.com. You should carefully read the following terms and conditions before using this Book. Unless you have a different license agreement obtained from http://curl.phptrack.com, 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 http://curl.phptrack.com. All rights to the Book and its Examples and documentation not expressly granted under this Agreement are reserved to http://curl.phptrack.com. 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   # "   "    -  & -     [...]... curl_ setopt ($ch, CURLOPT_URL,$url); curl_ setopt($ch, CURLOPT_HEADER, 1); curl_ setopt ($ch, CURLOPT_POST, 1); curl_ setopt ($ch, CURLOPT_POSTFIELDS, $xml); curl_ setopt ($ch, CURLOPT_SSLCERT, $key); curl_ setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_ setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_ setopt ($ch, CURLOPT_RETURNTRANSFER, 1); if ($debugging) curl_ setopt ($ch, CURLOPT_VERBOSE, 1); # use curl to send... $ftp_url); curl_ setopt($ch, CURLOPT_UPLOAD, 1); curl_ setopt($ch, CURLOPT_INFILE, $fp); curl_ setopt($ch, CURLOPT_INFILESIZE, $file_size); $result = curl_ exec ($ch); curl_ close ($ch); print $result; ?> " PHP /CURL Book with Examples 4 6 ( 0 $ % & & &- ( - - + < ?php // http:/ /curl. phptrack.com // PHP /CURL FTP delete a remote file // Copyright imran@phptrack.com $ftp_url = "ftp://id:password@phptrack.com/public_html /curl/ examples/";... Close"; $ch = curl_ init(); curl_ setopt($ch, CURLOPT_URL,$url); curl_ setopt($ch, CURLOPT_HTTPHEADER, $header_array); curl_ setopt($ch, CURLOPT_REFERER, $reffer); curl_ setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_ setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_ setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_ setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); $result = curl_ exec ($ch); curl_ close ($ch);... &- ) PHP /CURL Book with Examples < ?php // Example 005 // Pass Refferal to the Target Site This insure that request is from this site // Copyright http:/ /curl. phptrack.com $url = "http:/ /curl. phptrack.com/login .php" ; // URL $reffer = "http:/ /curl. phptrack.com/index .php" ; // Refferal site $ch = curl_ init(); // Initialize a CURL session curl_ setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter curl_ setopt($ch,... $result = curl_ exec ($ch); curl_ close ($ch); print $result; ?> " PHP /CURL Book with Examples 5 # 1 & CURLOPT_AUTOREFERER TRUE CURLOPT_BINARYTRANSFER TRUE CURLOPT_RETURNTRANSFER TRUE ! " CURLOPT_COOKIESESSION # # $ % % CURLOPT_CRLF TRUE CURLOPT_DNS_USE_GLOBAL_CACHE TRUE CURLOPT_FAILONERROR ! & % ! ') (* + $ , - TRUE - ! / ! #! ! TRUE 0 CURLOPT_FILETIME ! TRUE - CURLOPT_FOLLOWLOCATION CURLOPT_MAXREDIRS CURLOPT_FORBID_REUSE... CURLOPT_FORBID_REUSE CURLOPT_FRESH_CONNECT CURLOPT_FTP_USE_EPRT CURLOPT_FTP_USE_EPSV CURLOPT_FTPAPPEND CURLOPT_FTPASCII 1 # # 2 TRUE % ! # TRUE TRUE 3( 1 )( 2 & FALSE 4( - * )( - TRUE 3$ 5 ! 3$ 5 FALSE ! 3( * $ $ 5 TRUE ! CURLOPT_TRANSFERTEXT & PHP /CURL Book with Examples CURLOPT_FTPLISTONLY TRUE CURLOPT_HEADER TRUE CURLOPT_HTTPGET * - TRUE $ 6 3 6 3 # ! CURLOPT_HTTPPROXYTUNNEL TRUE CURLOPT_MUTE TRUE CURLOPT_NETRC... & & 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 http:/ /curl. phptrack.com $url = "http:/ /curl. phptrack.com/login .php" ; // URL $ch = curl_ init(); // Initialize a CURL session curl_ setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter curl_ setopt($ch,... ?> PHP /CURL Book with Examples 4 5 ( 0 % & & &- ( - - + < ?php // http:/ /curl. phptrack.com // PHP /CURL FTP upload to a remote site // Copyright imran@phptrack.com $ftp_url = "ftp://id:password@phptrack.com/public_html/cu/dt.jpg"; $file_to_upload_path = realpath("product.jpg"); $file_size = filesize($file_to_upload_path); $fp = fopen($file_to_upload_path,'rb'); $ch = curl_ init(); curl_ setopt($ch, CURLOPT_URL,... - P & # E # PHP /CURL Book with Examples 8 ( ' - #@ % ! + 2 < ?php // Example 002.1 // Pass Form Variables as method = GET // Copyright http:/ /curl. phptrack.com $domain = "http:/ /curl. phptrack.com/"; // 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,... ) ! - " - "- "A < ?php // Example 003 // Download Image (Binnary File) // Copyright http:/ /curl. phptrack.com $url = "http:/ /curl. phptrack.com/images/header.jpg"; // 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, . 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. "http:/ /curl. phptrack.com/login .php& quot;; // URL $reffer = "http:/ /curl. phptrack.com/index .php& quot;; // Refferal site $ch = curl_ init(); // Initialize a CURL session. curl_ setopt($ch, CURLOPT_URL,. June, 20 05,Version 1.5, Jan, 20 06,Version 1.7, Mar, 20 06, Version 1.8, Mar, 20 06. This document is subject to change without notice. +,(  PHP /CURL Book Copyright 20 05 -20 06 http:/ /curl. phptrack.com

Ngày đăng: 17/06/2015, 15:43

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan