File: fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch11_cURL_to_Interact_with_WebServ/getpage.php

Recommend this page to a friend!
  Classes of Slavko Srakocic  >  B12 PHP FW  >  fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch11_cURL_to_Interact_with_WebServ/getpage.php  >  Download  
File: fwphp/glomodul/z_examples/book_video/03steinmetz_2008/ch11_cURL_to_Interact_with_WebServ/getpage.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: B12 PHP FW
Manage database records with a PDO CRUD interface
Author: By
Last change:
Date: 2 months ago
Size: 1,226 bytes
 

Contents

Class file image Download
<?
function retrieve_page($url, $post_parameters = null) {
   
/* connects to a site using POST or GET; fetches data */
   
$query_string = null;
    if (!
is_null($post_parameters)) {
    if (!
is_array($post_parameters)) {
        die(
"POST parameters not in array format");
    }
   
/* build query string */
   
$query_string = http_build_query($post_parameters);
    }

   
$ch = curl_init();

    if (
$query_string) {
   
curl_setopt($ch, CURLOPT_POST, true);
   
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
    }

   
curl_setopt($ch, CURLOPT_URL, $url);
   
curl_setopt($ch, CURLOPT_HEADER, false);
   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   
$return_data = curl_exec($ch);
   
curl_close($ch);
    return
$return_data;
}

$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://www.google.com/search?q=beans");
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$page_data = curl_exec($c);
curl_close($c);
print
$page_data;

// $foo = retrieve_page("http://www.google.com/search?q=beans");
// $foo = retrieve_page("http://www.google.com/search", array("q" => "beans"));
// print $foo;

$foo = retrieve_page("http://search.yahoo.com/search", array("p" => "beans"));
print
$foo;

?>

For more information send a message to info at phpclasses dot org.