curl_init

(PHP 4 >= 4.0.2, PHP 5, PHP 7, PHP 8)

curl_initcURL セッションを初期化する

説明

curl_init(string|null $url = null): CurlHandle|false

新規セッションを初期化し、cURL ハンドルを返します。このハンドルは、関数 curl_setopt(), curl_exec(), curl_close() で使用します。

パラメータ

url

urlを指定した場合、オプション CURLOPT_URL がそのパラメータの値に設定されます。関数 curl_setopt() により、 この値をマニュアルで設定することも可能です。

注意:

open_basedir が設定されている場合、cURL で file プロトコルは使えなくなります。

返り値

成功した場合に cURL ハンドル、エラー時に false を返します。

変更履歴

バージョン 説明
8.0.0 成功時に、この関数は CurlHandle クラスのインスタンスを返すようになりました。 これより前のバージョンでは、resource を返していました。
8.0.0 url は、nullable になりました。

例1 新しい cURL セッションを初期化し、ウェブページを取得する

<?php
// 新しい cURL リソースを作成します
$ch curl_init();

// URL や他の適当なオプションを設定します
curl_setopt($chCURLOPT_URL"http://www.example.com/");
curl_setopt($chCURLOPT_HEADER0);

// URL を取得し、ブラウザに渡します
curl_exec($ch);

// cURL リソースを閉じ、システムリソースを解放します
curl_close($ch);
?>

参考

add a note add a note

User Contributed Notes 1 note

up
1
webmaster at jamescobban dot net
3 years ago
On recent distributions CURL and PHP support for CURL have not been included in the main product.  In particular in recent distributions of Ubuntu Linux CURL and PHP support for CURL are not even available from the official repositories.  The steps to incorporate support are complex and require adding a non-standard repository.  It is therefore advisable for programmers to rewrite code to use the stream interface to access resources across the Internet.  For example:

```php
$opts = array(
        'http' => array (
            'method'=>"POST",
            'header'=>
              "Accept-language: en\r\n".
              "Content-type: application/x-www-form-urlencoded\r\n",
            'content'=>http_build_query(array('foo'=>'bar'))
  )
);

$context = stream_context_create($opts);

$fp = fopen('https://www.example.com', 'r', false, $context);

```

This stream support can also be accessed using the object-oriented interface of SplFileObject.
To Top