Results 1 to 3 of 3

Thread: cURL in PHP

  1. #1
    Administrator Jennifer's Avatar
    Join Date
    Jul 2010
    Posts
    99
    ΑΡ Credit
    990

    cURL in PHP

    What is cURL in PHP?

    cURL is a library that allows PHP applications to communicate with other servers over several protocols.

    PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

    PHP Code:
    <?php
    $ch 
    curl_init();
    curl_setopt($chCURLOPT_URL"http://example.com/");
    curl_setopt($chCURLOPT_HEADER0);
    curl_exec($ch);
    curl_close($ch);
    ?>
    cURL is a library which allows you to connect and communicate to many different types of servers with many different types of protocols. Using cURL you can:
    • Implement payment gateways’ payment notification scripts.
    • Download and upload files from remote servers.
    • Login to other websites and access members only sections.

    PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up a major parts of its functionality in just four functions.A typical PHP cURL usage follows the following sequence of steps.curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.
    curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.
    curl_exec – Executes a cURL session.
    curl_close – Closes the current cURL session.
    Below are some examples which should make the working of cURL more clearer.Download file or web page using PHP cURL

    The below piece of PHP code uses cURL to download Google’s RSS feed.


    PHP Code:
    /**  * Initialize the cURL session  */  
    $ch curl_init();  
     
    /**  * Set the URL of the page or file to download.  */  
    curl_setopt($chCURLOPT_URL'http://news.google.com/news?hl=en&topic=t& output=rss');  
     
    /**  * Ask cURL to return the contents in a variable instead of simply echoing them to  the browser.  */  

    curl_setopt($chCURLOPT_RETURNTRANSFER1);   
    /**  * Execute the cURL session  */  
    $contents curl_exec ($ch);  
     
    /**  * Close cURL session  */  
    curl_close ($ch); 
    As you can see, curl_setopt is the pivot around which the main cURL functionality revolves. cURL functioning is controlled by way of passing predefined options and values to this function.The above code uses two such options.
    • CURLOPT_URL: Use it to specify the URL which you want to process. This could be the URL of the file you want to download or it could be the URL of the script to which you want to post some data.
    • CURLOPT_RETURNTRANSFER: Setting this option to 1 will cause the curl_exec function to return the contents instead of echoing them to the browser.

    Download file or web page using PHP cURL and save it to file

    The below PHP code is a slight variation of the above code. It not only downloads the contents of the specified URL but also saves it to a file.

    PHP Code:
     /**  * Initialize the cURL session  */  
    $ch curl_init(); 
     
    /**  * Set the URL of the page or file to download.  */ 
     
    curl_setopt($chCURLOPT_URL'http://news.google.com/news?hl=en&topic=t& output=rss');   
    /**  * Create a new file  */ 
     
    $fp fopen('rss.xml’, 'w&#8217;);  
     /**  * Ask cURL to write the contents to a file  */ 
     
    curl_setopt($chCURLOPT_FILE$fp);   
    /**  * Execute the cURL session  */  
    curl_exec ($ch);  
     
    /**  * Close cURL session and file  */  
    curl_close ($ch);  fclose($fp); 
    Here we have used another of the cURL options, CURLOPT_FILE. Obtain a file handler by creating a new file or opening an existing one and then pass this file handler to the curl_set_opt function.cURL will now write the contents to a file as it downloads a web page or file.

  2. #2
    Administrator Jennifer's Avatar
    Join Date
    Jul 2010
    Posts
    99
    ΑΡ Credit
    990

    Using curl to Query Remote Servers

    Curl Introduction

    curl is the client URL function library. PHP supports it through libcurl. To enable support for libcurl when installing PHP add --with-curl=[location of curl libraries] to the configure statement before compiling. The curl package must be installed prior to installing PHP. Most major functions desired when connecting to remote web servers are included in curl, including POST and GET form posting, SSL support, HTTP authentication, session and cookie handling.

    To start a curl session use the curl_init() function. Options for the curl session are set via the curl_setopt() PHP function. Once you have the options set execute the request with the curl_exec() function.
    Example:
    PHP Code:
    <? 
    $ch 
    curl_init();
    curl_setopt($chCURLOPT_URL'http://www.example.com');
    curl_setopt($chCURLOPT_HEADER1);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    $data curl_exec();
    curl_close($ch);
     
    ?>
    In the previous example we first set the handle of the curl session to $ch. Then with the curl_setopt command we set the URL of the request to
    Code:
    www.example.com
    The CURLOPT_HEADER option sets whether or not the server response header should be returned. By default curl will display the response straight to the browser as the script is executed. To counter this we enabled the CURLOPT_RETURNTRANSFER option. Now when we run the curl_exec() statement the data returned from the remote server is returned and stored the the $data variable instead of passed to the browser.


    Curl and form data

    Okay now that we can pull static pages from remote servers lets move into posting information into web forms automatically. The default method of sending form data with curl is GET. In the following example we'll send a text message to our cell phone via the web form. Our example web form will require data in the POST format and contains the fields pNUMBER , MESSAGE, SUBMIT.
    PHP Code:
    <? 
    $phoneNumber 
    '4045551111';
    $message 'This message was generated by curl and php';
    $curlPost 'pNUMBER='  urlencode($phoneNumber) . '&MESSAGE=' urlencode($message) . '&SUBMIT=Send';
    $ch curl_init();
    curl_setopt($chCURLOPT_URL'http://www.example.com/sendSMS.php');
    curl_setopt($chCURLOPT_HEADER1);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_POST1);
    curl_setopt($chCURLOPT_POSTFIELDS$curlPost);
    $data curl_exec();
    curl_close($ch);
     
    ?>
    Here we set the phone number and message for the form into variables. The $curlPost variable is being used to store the POST data curl will use. When forming the $curlPost variable which will be used by curl_setopt later be sure to urlencode your data prior to passing it to curl_setopt. CURLOPT_POST is the option used to tell curl to send the form response via the POST method. CURLOPT_POSTFIELDS is the curl option used to store the POST data.


    Curl and proxies

    As with all other full featured browsers curl has support for proxies. Proxy servers are buffers between the requesting client and the web server. Proxy servers are used for a variety of reasons including companies restricting web access to people wanting to appear anonymous.

    There are a few curl options to set when using a proxy. First to enable use of a proxy in curl use the option CURLOPT_HTTPPROXYTUNNEL. Second set the proxy with the option CURLOPT_PROXY. If you need to set authentication information use the option CURLOPT_PROXYUSERPWD. CURLOPT_PROXYUSERPWD expects a string in the format of user: password. HTTP proxies are default, to use a SOCKS proxy use the CURLOPT_PROXYTYPE option.
    Example:
    PHP Code:
    <? 
    $ch 
    curl_init();
    curl_setopt($chCURLOPT_URL'http://www.example.com');
    curl_setopt($chCURLOPT_HEADER1);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_HTTPPROXYTUNNEL1);
    curl_setopt($chCURLOPT_PROXY'fakeproxy.com:1080');
    curl_setopt($chCURLOPT_PROXYUSERPWD'user:password');
    $data curl_exec();
    curl_close($ch);
     
    ?>

    Using SSL and cookies in curl
    SSL

    To enable SSL simply change http:// to https:// in the CURLOPT_URL option. Some common options for curl when using SSL are CURLOPT_SSL_VERIFYHOST which when set to 1 verifies the existence of a common name in the certificate and if set to 2 verifies the existence of a common name and that the common name in the certificate matches the host name of the server. CURLOPT_SSLVERSION can be used to switch between SSL version 2 and 3, this is normally auto negotiated by PHP.

    Cookies

    Curl also has support for cookies. Cookie handling with curl is very simple. There are three options built into curl, CURLOPT_COOKIE, CURLOPT_COOKIEJAR, and CURLOPT_COOKIEFILE. CURLOPT_COOKIE is used to set a cookie for the current session. CURLOPT_COOKIEJAR stores the location of a file to store the cookies received when the session is closed. CURLOPT_COOKIEFILE stores the location of a file containing cookies written in either Netscape format or raw HTTP header style.

    Authenticating with curl

    All popular methods of HTTP authentication are supported, including HTTP basic, digest, GSS and NTLM. As with other curl setting the curl_setopt function is used. To set the authentication method do curl_setopt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); this would set basic authentication. Multiple authentication types can be selected via the | or selector. The credentials are set with curl_setopt(CURLOPT_USERPWD, '[username]:[password]').

    The other authentication options are CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, CURLAUTH_NTLM for digest, GSS negotiate, NTLM respectively. Along with those there are also CURLAUTH_ANY which is an alias to CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_GSSNEGOTIA TE|CURLAUTH_NTLM and CURLAUTH_ANYSAFE which aliases to CURLAUTH_DIGEST|CURLAUTH_GSSNEGOTIATE|CURLAUTH_NTL M.
    Example 1:
    PHP Code:
    <? 
    $ch 
    curl_init();
    curl_setopt($chCURLOPT_URL'http://www.example.com');
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);
    curl_setopt(CURLOPT_USERPWD'[username]:[password]')

    $data curl_exec();
    curl_close($ch);
     
    ?>
    Example 2:
    PHP Code:
    <? 
    $ch 
    curl_init();
    curl_setopt($chCURLOPT_URL'http://www.example.com');
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_ANY);
    curl_setopt(CURLOPT_USERPWD'[username]:[password]')

    $data curl_exec();
    curl_close($ch);
     
    ?>
    In the first example we are just attempting to authenticate with basic authentication but in the second example we're going to try all possible authentication methods.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    1
    ΑΡ Credit
    10
    Hello, I know this is an old thread, but i'm really need your helps.
    I already tried all codes you post in here but none works.

    I want to used curl to bypass http authentication and save file to my pc.

    For example:
    url is hxxp://www.example. com/download.php/files.zip
    when open this link, it will require username and password.

    I need a php script that allow me to put the download url, username, and password, for download the file to local hard disk.

    and can you please tell me what php modules must be enabled to make it works?

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •