PHP/CURL Book with Examples - Welcome to PHP/CURL Book- Getting Started with CURL PHP Book- PHP/CURL Functions with Examples
!"#$" %&' ((( )*((( 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% @ - " B8 & & & & & - ) > @# - ># - " B ) & & & & ) " B& & & & & " & - B & & & - ! > & ! % >& ) ") & PHP/CURL Book with Examples 2/ # / 2/ # / 2/ # / 2/ # / 8 - = & C - D 1 $ & ) " A # - "' E & " & & +F ; 25 5 6 6 2; 5 5 "+F ; 2345 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 . / / - 25 & & - $ % & "' . & - # & ? / =& & ) " & & & ( + # # ) & - =& & # . - - & & - & $ % 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. 22 & ? & 22 & ? & 22 & ? & 22 & ? & ) # ! & & ! ) "& ) % & - & - & ! - & ) ) / ) & - I =& - & & ) & - & ! * + ,? ) 9 2@# ! & & ) % & # % & 9 2@# =& 423") & - % & 9 ; ! ( 43") & - % & J 9 ; : ! 5 ) ? % & ! & 9 5 23=& & 23=& &23=& & 23=& & J ) @@ @ KL $ =M $ = & - ) & & & ! # & - & - - =& >& - >- ) - # - & - > > - & & ) & - - # & - # - & ># > - ) ! & & & ! 43) & & ! @@ @ 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 33 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 # " " - & - . FORCE AND EFFECT. PHP/CURL Book with Examples 1.0 Welcome to PHP/CURL Book ………………………………….………. 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