新闻说经济 發表於 2019-9-20 17:19:00

php curl 发起get和post网络请求

<h2>curl介绍</h2>
<p>curl是一个开源的网络链接库,支持http, https, ftp, gopher, telnet, dict, file, and ldap 协议。之前均益介绍了python版本的pycurl http://junyiseo.com/python/607.html ,现在介绍怎么使用php版本的URL.</p>
<h2>curl get请求</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span> curl_get(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">){

   </span><span style="color: rgba(128, 0, 128, 1)">$header</span> = <span style="color: rgba(0, 0, 255, 1)">array</span><span style="color: rgba(0, 0, 0, 1)">(
       </span>'Accept: application/json',<span style="color: rgba(0, 0, 0, 1)">
    );
    </span><span style="color: rgba(128, 0, 128, 1)">$curl</span> =<span style="color: rgba(0, 0, 0, 1)"> curl_init();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置抓取的url</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_URL, <span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置头文件的信息作为数据流输出</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_HEADER, 0<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 超时设置,以秒为单位</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_TIMEOUT, 1<span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 超时设置,以毫秒为单位
    // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

    // 设置请求头</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_HTTPHEADER, <span style="color: rgba(128, 0, 128, 1)">$header</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置获取的信息以文件流的形式返回,而不是直接输出。</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_RETURNTRANSFER, 1<span style="color: rgba(0, 0, 0, 1)">);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_SSL_VERIFYHOST, <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行命令</span>
    <span style="color: rgba(128, 0, 128, 1)">$data</span> = curl_exec(<span style="color: rgba(128, 0, 128, 1)">$curl</span><span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 显示错误信息</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (curl_error(<span style="color: rgba(128, 0, 128, 1)">$curl</span><span style="color: rgba(0, 0, 0, 1)">)) {
      </span><span style="color: rgba(0, 0, 255, 1)">print</span> "Error: " . curl_error(<span style="color: rgba(128, 0, 128, 1)">$curl</span><span style="color: rgba(0, 0, 0, 1)">);
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印返回的内容</span>
      <span style="color: rgba(0, 128, 128, 1)">var_dump</span>(<span style="color: rgba(128, 0, 128, 1)">$data</span><span style="color: rgba(0, 0, 0, 1)">);
      curl_close(</span><span style="color: rgba(128, 0, 128, 1)">$curl</span><span style="color: rgba(0, 0, 0, 1)">);
    }
}</span></pre>
</div>
<h2>curl post请求</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $url 是请求的链接
// $postdata 是传输的数据,数组格式</span>
<span style="color: rgba(0, 0, 255, 1)">function</span> curl_post( <span style="color: rgba(128, 0, 128, 1)">$url</span>, <span style="color: rgba(128, 0, 128, 1)">$postdata</span><span style="color: rgba(0, 0, 0, 1)"> ) {

   </span><span style="color: rgba(128, 0, 128, 1)">$header</span> = <span style="color: rgba(0, 0, 255, 1)">array</span><span style="color: rgba(0, 0, 0, 1)">(
       </span>'Accept: application/json',<span style="color: rgba(0, 0, 0, 1)">
    );

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">初始化</span>
    <span style="color: rgba(128, 0, 128, 1)">$curl</span> =<span style="color: rgba(0, 0, 0, 1)"> curl_init();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置抓取的url</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_URL, <span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置头文件的信息作为数据流输出</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_HEADER, 0<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置获取的信息以文件流的形式返回,而不是直接输出。</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_RETURNTRANSFER, 1<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 超时设置</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_TIMEOUT, 10<span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 超时设置,以毫秒为单位
    // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

    // 设置请求头</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_HTTPHEADER, <span style="color: rgba(128, 0, 128, 1)">$header</span><span style="color: rgba(0, 0, 0, 1)">);

    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: rgba(0, 0, 255, 1)">FALSE</span><span style="color: rgba(0, 0, 0, 1)"> );
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_SSL_VERIFYHOST, <span style="color: rgba(0, 0, 255, 1)">FALSE</span><span style="color: rgba(0, 0, 0, 1)"> );

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置post方式提交</span>
    curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_POST, 1<span style="color: rgba(0, 0, 0, 1)">);
    curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$curl</span>, CURLOPT_POSTFIELDS, <span style="color: rgba(128, 0, 128, 1)">$postdata</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行命令</span>
    <span style="color: rgba(128, 0, 128, 1)">$data</span> = curl_exec(<span style="color: rgba(128, 0, 128, 1)">$curl</span><span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 显示错误信息</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (curl_error(<span style="color: rgba(128, 0, 128, 1)">$curl</span><span style="color: rgba(0, 0, 0, 1)">)) {
      </span><span style="color: rgba(0, 0, 255, 1)">print</span> "Error: " . curl_error(<span style="color: rgba(128, 0, 128, 1)">$curl</span><span style="color: rgba(0, 0, 0, 1)">);
    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印返回的内容</span>
      <span style="color: rgba(0, 128, 128, 1)">var_dump</span>(<span style="color: rgba(128, 0, 128, 1)">$data</span><span style="color: rgba(0, 0, 0, 1)">);
      curl_close(</span><span style="color: rgba(128, 0, 128, 1)">$curl</span><span style="color: rgba(0, 0, 0, 1)">);
    }
}</span></pre>
</div>
<p>&nbsp;</p>
<h2>常用参数</h2>
<table>
<tbody>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</tbody>
<thead>
<tr><th>选项</th><th>将&nbsp;value&nbsp;设置为</th><th>备注</th></tr>
</thead>
<tbody>
<tr>
<td>CURLOPT_AUTOREFERER</td>
<td>TRUE&nbsp;时将根据&nbsp;<em>Location:</em>&nbsp;重定向时,自动设置 header 中的<em>Referer:</em>信息。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_BINARYTRANSFER</td>
<td>设为&nbsp;TRUE&nbsp;,将在启用&nbsp;CURLOPT_RETURNTRANSFER&nbsp;时,返回原生的(Raw)输出。</td>
<td>从 PHP 5.1.3 开始,此选项不再有效果:使用CURLOPT_RETURNTRANSFER&nbsp;后总是会返回原生的(Raw)内容。</td>
</tr>
<tr>
<td>CURLOPT_COOKIESESSION</td>
<td>设为&nbsp;TRUE&nbsp;时将开启新的一次 cookie 会话。它将强制 libcurl 忽略之前会话时存的其他 cookie。 libcurl 在默认状况下无论是否为会话,都会储存、加载所有 cookie。会话 cookie 是指没有过期时间,只存活在会话之中。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_CERTINFO</td>
<td>TRUE&nbsp;将在安全传输时输出 SSL 证书信息到&nbsp;<em>STDERR</em>。</td>
<td>在 cURL 7.19.1 中添加。 PHP 5.3.2 后有效。 需要开启&nbsp;CURLOPT_VERBOSE&nbsp;才有效。</td>
</tr>
<tr>
<td>CURLOPT_CONNECT_ONLY</td>
<td>TRUE&nbsp;将让库执行所有需要的代理、验证、连接过程,但不传输数据。此选项用于 HTTP、SMTP 和 POP3。</td>
<td>在 7.15.2 中添加。 PHP 5.5.0 起有效。</td>
</tr>
<tr>
<td>CURLOPT_CRLF</td>
<td>启用时将Unix的换行符转换成回车换行符。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_DNS_USE_GLOBAL_CACHE</td>
<td>TRUE&nbsp;会启用一个全局的DNS缓存。此选项非线程安全的,默认已开启。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FAILONERROR</td>
<td>当 HTTP 状态码大于等于 400,TRUE&nbsp;将将显示错误详情。 默认情况下将返回页面,忽略 HTTP 代码。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_SSL_FALSESTART</td>
<td>TRUE&nbsp;开启 TLS False Start (一种 TLS 握手优化方式)</td>
<td>cURL 7.42.0 中添加。自 PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_FILETIME</td>
<td>TRUE&nbsp;时,会尝试获取远程文档中的修改时间信息。 信息可通过curl_getinfo()函数的CURLINFO_FILETIME&nbsp;选项获取。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FOLLOWLOCATION</td>
<td>TRUE&nbsp;时将会根据服务器返回 HTTP 头中的&nbsp;<em>"Location: "</em>&nbsp;重定向。(注意:这是递归的,<em>"Location: "</em>&nbsp;发送几次就重定向几次,除非设置了&nbsp;CURLOPT_MAXREDIRS,限制最大重定向次数。)。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FORBID_REUSE</td>
<td>TRUE&nbsp;在完成交互以后强制明确的断开连接,不能在连接池中重用。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FRESH_CONNECT</td>
<td>TRUE&nbsp;强制获取一个新的连接,而不是缓存中的连接。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FTP_USE_EPRT</td>
<td>TRUE&nbsp;时,当 FTP 下载时,使用 EPRT (和 LPRT)命令。 设置为&nbsp;FALSE&nbsp;时禁用 EPRT 和 LPRT,仅仅使用PORT 命令。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FTP_USE_EPSV</td>
<td>TRUE&nbsp;时,在FTP传输过程中,回到 PASV 模式前,先尝试 EPSV 命令。设置为&nbsp;FALSE&nbsp;时禁用 EPSV。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FTP_CREATE_MISSING_DIRS</td>
<td>TRUE&nbsp;时,当 ftp 操作不存在的目录时将创建它。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FTPAPPEND</td>
<td>TRUE&nbsp;为追加写入文件,而不是覆盖。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_TCP_NODELAY</td>
<td>TRUE&nbsp;时禁用 TCP 的 Nagle 算法,就是减少网络上的小包数量。</td>
<td>PHP 5.2.1 有效,编译时需要 libcurl 7.11.2 及以上。</td>
</tr>
<tr>
<td>CURLOPT_FTPASCII</td>
<td>CURLOPT_TRANSFERTEXT&nbsp;的别名。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_FTPLISTONLY</td>
<td>TRUE&nbsp;时只列出 FTP 目录的名字。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_HEADER</td>
<td>启用时会将头文件的信息作为数据流输出。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLINFO_HEADER_OUT</td>
<td>TRUE&nbsp;时追踪句柄的请求字符串。</td>
<td>从 PHP 5.1.3 开始可用。CURLINFO_&nbsp;的前缀是有意的(intentional)。</td>
</tr>
<tr>
<td>CURLOPT_HTTPGET</td>
<td>TRUE&nbsp;时会设置 HTTP 的 method 为 GET,由于默认是 GET,所以只有 method 被修改时才需要这个选项。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_HTTPPROXYTUNNEL</td>
<td>TRUE&nbsp;会通过指定的 HTTP 代理来传输。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_MUTE</td>
<td>TRUE&nbsp;时将完全静默,无论是何 cURL 函数。</td>
<td>在 cURL 7.15.5 中移出(可以使用 CURLOPT_RETURNTRANSFER 作为代替)</td>
</tr>
<tr>
<td>CURLOPT_NETRC</td>
<td>TRUE&nbsp;时,在连接建立时,访问<var>~/.netrc</var>文件获取用户名和密码来连接远程站点。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_NOBODY</td>
<td>TRUE&nbsp;时将不输出 BODY 部分。同时 Mehtod 变成了 HEAD。修改为&nbsp;FALSE时不会变成 GET。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_NOPROGRESS</td>
<td>
<p>TRUE&nbsp;时关闭 cURL 的传输进度。</p>
<blockquote>
<p>Note:</p>
<p>PHP 默认自动设置此选项为&nbsp;TRUE,只有为了调试才需要改变设置。</p>
</blockquote>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_NOSIGNAL</td>
<td>TRUE&nbsp;时忽略所有的 cURL 传递给 PHP 进行的信号。在 SAPI 多线程传输时此项被默认启用,所以超时选项仍能使用。</td>
<td>cURL 7.10时被加入。</td>
</tr>
<tr>
<td>CURLOPT_PATH_AS_IS</td>
<td>TRUE&nbsp;不处理 dot dot sequences (即 ../ )</td>
<td>cURL 7.42.0 时被加入。 PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_PIPEWAIT</td>
<td>TRUE&nbsp;则等待 pipelining/multiplexing。</td>
<td>cURL 7.43.0 时被加入。 PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_POST</td>
<td>TRUE&nbsp;时会发送 POST 请求,类型为:<em>application/x-www-form-urlencoded</em>,是 HTML 表单提交时最常见的一种。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_PUT</td>
<td>TRUE&nbsp;时允许 HTTP 发送文件。要被 PUT 的文件必须在&nbsp;CURLOPT_INFILE和CURLOPT_INFILESIZE&nbsp;中设置。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_RETURNTRANSFER</td>
<td>TRUE&nbsp;将curl_exec()获取的信息以字符串返回,而不是直接输出。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_SAFE_UPLOAD</td>
<td>TRUE&nbsp;禁用&nbsp;<em>@</em>&nbsp;前缀在&nbsp;CURLOPT_POSTFIELDS&nbsp;中发送文件。 意味着&nbsp;<em>@</em>&nbsp;可以在字段中安全得使用了。 可使用&nbsp;CURLFile&nbsp;作为上传的代替。</td>
<td>PHP 5.5.0 中添加,默认值&nbsp;FALSE。 PHP 5.6.0 改默认值为&nbsp;TRUE。. PHP 7 删除了此选项, 必须使用 CURLFile interface 来上传文件。</td>
</tr>
<tr>
<td>CURLOPT_SASL_IR</td>
<td>TRUE&nbsp;开启,收到首包(first packet)后发送初始的响应(initial response)。</td>
<td>cURL 7.31.10 中添加,自 PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_SSL_ENABLE_ALPN</td>
<td>FALSE&nbsp;禁用 SSL 握手中的 ALPN (如果 SSL 后端的 libcurl 内建支持) 用于协商到 http2。</td>
<td>cURL 7.36.0 中增加, PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_SSL_ENABLE_NPN</td>
<td>FALSE&nbsp;禁用 SSL 握手中的 NPN(如果 SSL 后端的 libcurl 内建支持),用于协商到 http2。</td>
<td>cURL 7.36.0 中增加, PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_SSL_VERIFYPEER</td>
<td>FALSE&nbsp;禁止 cURL 验证对等证书(peer's certificate)。要验证的交换证书可以在&nbsp;CURLOPT_CAINFO&nbsp;选项中设置,或在&nbsp;CURLOPT_CAPATH中设置证书目录。</td>
<td>自cURL 7.10开始默认为&nbsp;TRUE。从 cURL 7.10开始默认绑定安装。</td>
</tr>
<tr>
<td>CURLOPT_SSL_VERIFYSTATUS</td>
<td>TRUE&nbsp;验证证书状态。</td>
<td>cURL 7.41.0 中添加, PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_TCP_FASTOPEN</td>
<td>TRUE&nbsp;开启 TCP Fast Open。</td>
<td>cURL 7.49.0 中添加, PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_TFTP_NO_OPTIONS</td>
<td>TRUE&nbsp;不发送 TFTP 的 options 请求。</td>
<td>自 cURL 7.48.0 添加, PHP 7.0.7 起有效。</td>
</tr>
<tr>
<td>CURLOPT_TRANSFERTEXT</td>
<td>TRUE&nbsp;对 FTP 传输使用 ASCII 模式。对于LDAP,它检索纯文本信息而非 HTML。在 Windows 系统上,系统不会把&nbsp;<em>STDOUT</em>&nbsp;设置成二进制 模式。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_UNRESTRICTED_AUTH</td>
<td>TRUE&nbsp;在使用CURLOPT_FOLLOWLOCATION重定向 header 中的多个 location 时继续发送用户名和密码信息,哪怕主机名已改变。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_UPLOAD</td>
<td>TRUE&nbsp;准备上传。</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>CURLOPT_VERBOSE</td>
<td>TRUE&nbsp;会输出所有的信息,写入到<em>STDERR</em>,或在CURLOPT_STDERR中指定的文件。</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table><br><br>
来源:https://www.cnblogs.com/niuben/p/11558420.html
頁: [1]
查看完整版本: php curl 发起get和post网络请求