Sunteți pe pagina 1din 4

<?

php /* Here is a script that is usefull to : - login to a POST form, - store a session cookie, - download a file once logged in. */ // INIT CURL $ch = curl_init(); // SET URL FOR THE POST FORM LOGIN curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/Members/Login.php'); // ENABLE HTTP POST curl_setopt ($ch, CURLOPT_POST, 1); // SET POST PARAMETERS : FORM VALUES FOR EACH FIELD curl_setopt ($ch, CURLOPT_POSTFIELDS, 'fieldname1=fieldvalue1&fieldname2=fieldvalue2'); // IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); # Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL # not to print out the results of its query. # Instead, it will return the results as a string return value # from curl_exec() instead of the usual true/false. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // EXECUTE 1st REQUEST (FORM LOGIN) $store = curl_exec ($ch); // SET FILE TO DOWNLOAD curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/Members/Downloads/AnnualReport.pdf'); // EXECUTE 2nd REQUEST (FILE DOWNLOAD) $content = curl_exec ($ch); // CLOSE CURL curl_close ($ch); /* At this point you can do do whatever you want with the downloaded file stored in $content : display it, save it as file, and so on. */ ?>

paul at zgtec dot com 26-Aug-2002 09:31


To make a POST in multipart/form-data mode this worked for me, the " \n" at the end of the variables was very important

on my OS X server. <?php $file = "file_to_upload.txt"; $submit_url = "http://www.example.com/upload_page.php"; $formvars = array("cc"=>"us \n"); $formvars[variable_1] = "bla bla \n"; $formvars[variable_2] = "bla bla \n"; $formvars[variable_3] = "bla bla \n"; $formvars[variable_4] = "bla bla \n"; $formvars[upfile] = "@$file"; // "@" causes cURL to send as file and not string (I believe) // init curl handle $ch = curl_init($submit_url); curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt"); //initiates cookie file if needed curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt"); // Uses cookies from previous session if exist curl_setopt($ch, CURLOPT_REFERER, "http://www.example.net"); //if server needs to think this post came from elsewhere curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $formvars); // perform post echo $pnp_result_page = curl_exec($ch); curl_close ($ch); ?>

<?php curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false); ?>

This worked great, but I was required to verify, so here's what I did. Add the following lines:

<?php curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 1);

curl_setopt($connection, CURLOPT_CAINFO, "path:/ca-bundle.crt"); ?>

================================================================ http://php.net/manual/en/function.curl-setopt.php

To fetch (or submit data to) multiple pages during one session,use this:

<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile"); curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile"); curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id()); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_URL, 'http://example.com/page1.php'); $result1 = curl_exec($ch);

curl_setopt($ch, CURLOPT_URL, 'http://example.com/page2.php'); $result2 = curl_exec($ch);

curl_close($ch); ?>

S-ar putea să vă placă și