Curl and curling from PHP
Archive - Originally posted on "The Horse's Mouth" - 2013-04-04 02:07:23 - Graham EllisThe Curl library collects a resource in a non-interactive way from a service... libcurl is a free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos), file transfer resume, http proxy tunnelling and more! says libcurl's home page. Bindings are currently offered to use the library within your program in Ada95 Basic C C++ Ch Cocoa D Dylan Eiffel Euphoria Falcon Ferite Gambas glib/GTK+ Guile Haskell ILE/RPG Java Lisp Lua Mono .NET Object-Pascal OCaml Pascal Perl PHP Postgres Python R Rexx Ruby Scheme S-Lang Smalltalk SP-Forth SPL Tcl Visual Basic Visual FoxPro Q wxWidgets and XBLite. There's a command line tool in Unix and Linux too with a hst of options - see [here] for examples of it in use.
On today's PHP course, we were accessing my service described in another recent article from another server layer, and we chose to use Curl. PHP needs to be built with the library included - To use PHP's cURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories. In the include directory there should be a folder named curl which should contain the easy.h and curl.h files. There should be a file named libcurl.a located in the lib directory. and the you can use the curl functions in your script:
$st = curl_init();
curl_setopt($st,CURLOPT_URL,"http://192.168.192.1/service.php?type=html");
curl_setopt($st,CURLOPT_RETURNTRANSFER,1);
// curl_setopt($st,CURLOPT_PROXY,"www-cache.reith.bbc.co.uk:80");
$data = curl_exec($st);
curl_close($st);
As with the command line Curl, there are dozens of settings you can specify with cure_setopt... you'll see a few above, and may also like to note CURLOPT_USERAGENT to set the browser identity string and CURLOPT_REFERER to indicate a previous page. Have a look at the PHP manual for the page for much more information!