|
| 1 | +<?xml version="1.0" encoding="utf-8"?> |
| 2 | +<!-- EN-Revision: 9e08f606a38fb7c0de98b42375636692d43d7040 Maintainer: mowangjuanzi Status: ready --> |
| 3 | +<refentry xml:id="function.curl-share-init-persistent" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> |
| 4 | + <refnamediv> |
| 5 | + <refname>curl_share_init_persistent</refname> |
| 6 | + <refpurpose>初始化 <emphasis role="bold">持久</emphasis> cURL 共享句柄</refpurpose> |
| 7 | + </refnamediv> |
| 8 | + |
| 9 | + <refsect1 role="description"> |
| 10 | + &reftitle.description; |
| 11 | + <methodsynopsis> |
| 12 | + <type>CurlSharePersistentHandle</type><methodname>curl_share_init_persistent</methodname> |
| 13 | + <methodparam><type>array</type><parameter>share_options</parameter></methodparam> |
| 14 | + </methodsynopsis> |
| 15 | + <simpara> |
| 16 | + 使用指定的共享选项初始化<emphasis role="bold">持久</emphasis> cURL 共享句柄。与 <function>curl_share_init</function> |
| 17 | + 不同,此函数创建的句柄不会在 PHP 请求结束时销毁。如果找到具有相同 <parameter>share_options</parameter> |
| 18 | + 的持久共享句柄,则会重用该句柄。 |
| 19 | + </simpara> |
| 20 | + </refsect1> |
| 21 | + |
| 22 | + <refsect1 role="parameters"> |
| 23 | + &reftitle.parameters; |
| 24 | + |
| 25 | + <variablelist> |
| 26 | + <varlistentry> |
| 27 | + <term><parameter>share_options</parameter></term> |
| 28 | + <listitem> |
| 29 | + <simpara> |
| 30 | + 非空的 <constant>CURL_LOCK_DATA_<replaceable>*</replaceable></constant> 常量数组。 |
| 31 | + </simpara> |
| 32 | + <note> |
| 33 | + <simpara> |
| 34 | + 不允许使用 <constant>CURL_LOCK_DATA_COOKIE</constant>,如果指定,此函数将抛出 <exceptionname>ValueError</exceptionname>。在 |
| 35 | + PHP 请求之间共享 cookies 可能导致用户之间的敏感 cookies 意外混淆。 |
| 36 | + </simpara> |
| 37 | + </note> |
| 38 | + </listitem> |
| 39 | + </varlistentry> |
| 40 | + </variablelist> |
| 41 | + |
| 42 | + </refsect1> |
| 43 | + |
| 44 | + <refsect1 role="returnvalues"> |
| 45 | + &reftitle.returnvalues; |
| 46 | + <simpara> |
| 47 | + 返回 <classname>CurlSharePersistentHandle</classname>。 |
| 48 | + </simpara> |
| 49 | + </refsect1> |
| 50 | + |
| 51 | + <refsect1 role="errors"> |
| 52 | + &reftitle.errors; |
| 53 | + <itemizedlist> |
| 54 | + <listitem> |
| 55 | + <simpara> |
| 56 | + 如果 <parameter>share_options</parameter> 为空,此函数抛出 <exceptionname>ValueError</exceptionname>。 |
| 57 | + </simpara> |
| 58 | + </listitem> |
| 59 | + <listitem> |
| 60 | + <simpara> |
| 61 | + 如果 <parameter>share_options</parameter> 包含与 <constant>CURL_LOCK_DATA_<replaceable>*</replaceable></constant> |
| 62 | + 不匹配的值,此函数将抛出 <classname>ValueError</classname>。 |
| 63 | + </simpara> |
| 64 | + </listitem> |
| 65 | + <listitem> |
| 66 | + <simpara> |
| 67 | + 如果 <parameter>share_options</parameter> 包含 <constant>CURL_LOCK_DATA_COOKIE</constant>,此函数将抛出 |
| 68 | + <exceptionname>ValueError</exceptionname>。 |
| 69 | + </simpara> |
| 70 | + </listitem> |
| 71 | + <listitem> |
| 72 | + <simpara> |
| 73 | + 如果 <parameter>share_options</parameter> 包含非整数值,此函数将抛出 <exceptionname>TypeError</exceptionname>。 |
| 74 | + </simpara> |
| 75 | + </listitem> |
| 76 | + </itemizedlist> |
| 77 | + </refsect1> |
| 78 | + |
| 79 | + <refsect1 role="examples"> |
| 80 | + &reftitle.examples; |
| 81 | + <example xml:id="function.curl-share-init-persistent.example.basic"> |
| 82 | + <title><function>curl_share_init_persistent</function> 示例</title> |
| 83 | + <simpara> |
| 84 | + 此示例将创建持久 cURL 共享句柄,并演示它们之间共享连接的功能。如果在长生命周期的 PHP SAPI |
| 85 | + 中执行,<literal>$sh</literal> 将在 SAPI 请求之间保持存在。 |
| 86 | + </simpara> |
| 87 | + |
| 88 | + <programlisting role="php"> |
| 89 | + <![CDATA[ |
| 90 | +<?php |
| 91 | +
|
| 92 | +// 创建或获取持久 cURL 共享句柄,设置为共享 DNS 查找和连接 |
| 93 | +$sh = curl_share_init_persistent([CURL_LOCK_DATA_DNS, CURL_LOCK_DATA_CONNECT]); |
| 94 | +
|
| 95 | +// 初始化第一个 cURL 句柄并为其分配共享句柄 |
| 96 | +$ch1 = curl_init("http://example.com/"); |
| 97 | +curl_setopt($ch1, CURLOPT_SHARE, $sh); |
| 98 | +
|
| 99 | +// 执行第一个 cURL 句柄。此操作可能会重用来自先前 SAPI 请求的连接 |
| 100 | +curl_exec($ch1); |
| 101 | +
|
| 102 | +// 初始化第二个 cURL 句柄并为其分配共享句柄 |
| 103 | +$ch2 = curl_init("http://example.com/"); |
| 104 | +curl_setopt($ch2, CURLOPT_SHARE, $sh); |
| 105 | +
|
| 106 | +// 执行第二个 cURL 句柄。此操作将重用来自 $ch1 的连接 |
| 107 | +curl_exec($ch2); |
| 108 | +
|
| 109 | +?> |
| 110 | +
|
| 111 | + ]]> |
| 112 | + </programlisting> |
| 113 | + </example> |
| 114 | + </refsect1> |
| 115 | + |
| 116 | + <refsect1 role="seealso"> |
| 117 | + &reftitle.seealso; |
| 118 | + <simplelist> |
| 119 | + <member><function>curl_setopt</function></member> |
| 120 | + <member><function>curl_share_init</function></member> |
| 121 | + </simplelist> |
| 122 | + </refsect1> |
| 123 | + |
| 124 | +</refentry> |
| 125 | +<!-- Keep this comment at the end of the file |
| 126 | +Local variables: |
| 127 | +mode: sgml |
| 128 | +sgml-omittag:t |
| 129 | +sgml-shorttag:t |
| 130 | +sgml-minimize-attributes:nil |
| 131 | +sgml-always-quote-attributes:t |
| 132 | +sgml-indent-step:1 |
| 133 | +sgml-indent-data:t |
| 134 | +indent-tabs-mode:nil |
| 135 | +sgml-parent-document:nil |
| 136 | +sgml-default-dtd-file:"~/.phpdoc/manual.ced" |
| 137 | +sgml-exposed-tags:nil |
| 138 | +sgml-local-catalogs:nil |
| 139 | +sgml-local-ecat-files:nil |
| 140 | +End: |
| 141 | +vim600: syn=xml fen fdm=syntax fdl=2 si |
| 142 | +vim: et tw=78 syn=sgml |
| 143 | +vi: ts=1 sw=1 |
| 144 | +--> |
0 commit comments