ホームページ  >  記事  >  バックエンド開発  >  phpのcurl_init関数の使用例

phpのcurl_init関数の使用例

WBOY
WBOYオリジナル
2016-07-25 08:54:24995ブラウズ
  1. //curlオブジェクトを初期化します
  2. $curl =curl_init();
  3. //クロールする必要があるURLを設定します
  4. curl_setopt($curl,curlopt_url, 'http://bbs.it -home.org');
  5. //ヘッダーを設定します
  6. curl_setopt($curl,curlopt_header, 1);
  7. //結果を文字列で保存するか画面に出力するかを尋ねるカールパラメータを設定します。
  8. curl_setopt($curl,curlopt_returntransfer, 1);
  9. //curlを実行してWebページをリクエスト
  10. $data =curl_exec($curl);
  11. //URLを閉じるリクエスト
  12. curl_close($curl);
  13. //取得したデータを表示
  14. var_dump($data);
  15. ?>
コードをコピー

例 2: データを投稿する sendsms.php は、2 つのフォーム フィールド (1 つは電話番号、もう 1 つはテキスト メッセージの内容) を受け入れることができます。 投稿データ

  1. $phonenumber ='13812345678';
  2. $message ='thisMessagewasgeneratedbycurlandphp';
  3. $curlpost='pnumber='.urlencode($phonenumber).'&message ='.urlencode($message ).'&submit=send';
  4. $ch =curl_init();
  5. curl_setopt($ch,curlopt_url, 'http://www.lxvoip.com/sendsms.php');
  6. curl_setopt($ch,curlopt_header, 1 );
  7. curl_setopt ($ch、curlopt_returntransfer、1);
  8. curl_setopt ($ch、curlopt_postfields、$curlpost);
  9. ?>
  10. コードをコピー
例 3: プロキシ サーバーの使用 プロキシサーバーを使用する

$ch =curl_init();
    curl_setopt($ch,curlopt_url, 'http://bbs.it-home.org'); );
  1. curl_setopt($ch,curlopt_returntransfer, 1);
  2. curl_setopt($ch,curlopt_httpproxytunnel, 1);
  3. $data =curl_exec();
  4. curl_close($ch);
  5. ? >
  6. コードをコピー
  7. 例 4: 模擬ログイン curl は、dz7.0 に適した discuz プログラムへのログインをシミュレートします。ユーザー名をユーザー名に、ユーザーパスをパスワードに変更するだけです。 curl はログイン discuz プログラムをシミュレートします
!extension_loaded('curl') && die('curl 拡張機能がロードされていません。'); $discuz_url = 'http://www.lxvoip.com';//フォーラムのアドレス $login_url = $discuz_url .'/logging.php?action=login';//ログインページのアドレス $get_url = $discuz_url .'/my.php?item=threads' //私の投稿
;

$post_fields = array(); //以下の 2 つの項目は変更する必要はありません $post_fields['ログインフィールド'] = 'ユーザー名'; $post_fields['loginsubmit'] = 'true'; //ユーザー名とパスワードを入力する必要があります $post_fields['ユーザー名'] = 'lxvoip'; $post_fields['パスワード'] = '88888888'; //秘密の質問 $post_fields['質問ID'] = 0; $post_fields['answer'] = ''; //@todo 確認コード $post_fields['seccoverify'] = '';

//フォームフォームハッシュを取得 $ch =curl_init($login_url); curl_setopt($ch,curlopt_header, 0); curl_setopt($ch,curlopt_returntransfer, 1); $contents =curl_exec($ch); カール_クローズ($ch); preg_match('/

/i', $contents, $matches); if(!empty($matches)) { $formhash = $matches[1]; } それ以外 { die('フォーラムハッシュが見つかりません。'); }

//データを投稿し、Cookieを取得します $cookie_file = ディレクトリ名(__file__) . '/cookie.txt'; //$cookie_file = tempnam('/tmp'); $ch =curl_init($login_url); curl_setopt($ch,curlopt_header,0); curl_setopt($ch,curlopt_returntransfer, 1); curl_setopt($ch,curlopt_post,1); curl_setopt($ch,curlopt_postfields,$post_fields); curl_setopt($ch,curlopt_cookiejar,$cookie_file); カール_exec($ch); カール_クローズ($ch);

//上記で取得した Cookie を使用して、表示するためにログインする必要があるページ コンテンツを取得します $ch =curl_init($get_url); curl_setopt($ch,curlopt_header,0); curl_setopt($ch,curlopt_returntransfer, 0); curl_setopt($ch,curlopt_cookiefile,$cookie_file); $contents =curl_exec($ch); カール_クローズ($ch);

var_dump($contents);

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。