I am a complete newbie to php curl. Can anyone tell me where to start? I need to import some information from another web page and store it in my database after some modifications.
5 に答える
デバッグが簡単!
すなわち
$curl = curl_init();
$URL="http://www.php.net";
curl_setopt($curl, CURLOPT_URL, $URL);
$contents = curl_exec($curl);
$httpcode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
$httpHeaders = curl_getinfo($curl);
curl_close($curl);
if($httpcode && $contents!='')
{
makeLog('calls ', $URL.' resp c:'.$httpcode.' r:'.$contents);
}
else if($httpcode)
{
makeLog('calls ', $URL.' resp c:'.$httpcode);
}
function makeLog($function , $message='')
{
$myFile = "errorLogs.txt";
$fh = fopen($myFile, 'a+');
fwrite($fh , "\n\r\n\r".$_SERVER['REMOTE_ADDR'].': '.$message.' -'.$function."\n\r\n\r");
}
これはあなたがここからすべてを得ることができる素晴らしい家庭教師です...
http://www.alfredfrancis.in/complete-php-curl-tutorial/
2.Webページをダウンロードするための単純なcUrlスクリプト。
<?php
$ch = curl_init();//starts curl handle
$Url="http://www.php.net";
curl_setopt($ch, CURLOPT_URL, $Url);//set url to download
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");//referrer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");//set user agent
curl_setopt($ch, CURLOPT_HEADER, 0);//include header in result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//should return data
curl_setopt($ch, CURLOPT_TIMEOUT, 20);//timeout in seconds
$output = curl_exec($ch);//ececute the request
curl_close($ch);
echo $output;//output the result
?>
PHP curl で何かを始める前に、.php ファイルを実行しているマシンに PHP curl がインストールされていますか?
そうでない場合:
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
次に、Apache を再起動します。
service apache2 restart
その後、次のコードをサーバー上の .php ファイルに配置して実行します。うまくいけば、右上隅に IP アドレスが表示されます。URL を他の Web サイトに変更してみてください。さらにアドバイスが必要な場合は、記事全体がここにあります: Beginners-cURL
<?php
// Initialize cURL
$ch = curl_init();
// Set the website you would like to scrape
curl_setopt($ch, CURLOPT_URL, "http://www.icanhazip.com");
// Set cURL to return the results into a PHP variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// This executes the cURL request and places the results into a variable.
$curlResults= curl_exec($ch);
// Close curl
curl_close($ch);
// Echo the results to the screen>
echo $curlResults;
?>
あなたが求めていたものとは正確には違いますが、cURL を使用せずに必要なものをほとんどすべて提供する優れた PHP クラスがあります。
これはSnoopyと呼ばれ、基本的にフォーム データ操作などの非常に便利な機能を備えたブラウザ エミュレート クラスです。
開始: http://php.net/manual/en/curl.examples-basic.php
出力を取得して操作する必要がある場合は、curl_setopt
関数 as を使用してcurl_setopt(CURLOPT_RETURNTRANSFER,TRUE);
ください。
出力を取得http://www.google.com
して画面に出力する例:
$ch = curl_init('http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$output = curl_exec($ch);
var_dump($output);