Following code returns the curl output as a string.
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
PHP を cURL サポート機能付きでコンパイルすると、 curl 関数を使用可能となります。cURL 関数の基本的な使用法は、 curl_init()により cURL セッションを初期化、 curl_setopt() により転送時のオプションを設定、 続いてcurl_exec() により転送を実行し、 curl_close() によりセッションを終了するというものになります。 cURL 関数を使用して example.com ホームページをファイルに取得する例を示します。
例1 PHP の cURL モジュールを使用して example.com のホームページを取得する
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
PHP の cURL モジュールを使用して example.com のホームページを取得する
cmnajs at gmail dot com
07-Jan-2009 11:57
07-Jan-2009 11:57
