php简单的curl示例(代码片段)

author author     2022-12-08     444

关键词:

<span style="color: #000000; font-weight: bold;">function</span> curl_download<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$Url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// is cURL installed yet?</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<a href="http://www.php.net/function_exists"><span style="color: #000066;">function_exists</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'curl_init'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Sorry cURL is not installed!'</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// OK cool - then let's create a new cURL resource handle</span>
    <span style="color: #0000ff;">$ch</span> = curl_init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Now set some options (most are optional)</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// Set URL to download</span>
    curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_URL, <span style="color: #0000ff;">$Url</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Set a referer</span>
    curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_REFERER, <span style="color: #ff0000;">&quot;http://www.example.org/yay.htm&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// User agent</span>
    curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_USERAGENT, <span style="color: #ff0000;">&quot;MozillaXYZ/1.0&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Include header in result? (0 = yes, 1 = no)</span>
    curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_HEADER, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Should cURL return or print out the data? (true = return, false = print)</span>
    curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Timeout in seconds</span>
    curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_TIMEOUT, <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Download the given URL, and return output</span>
    <span style="color: #0000ff;">$output</span> = curl_exec<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Close the cURL resource, and free system resources</span>
    curl_close<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$output</span>;
<span style="color: #66cc66;">&#125;</span>

php简单的xml示例(代码片段)

查看详情

php简单的http身份验证示例(代码片段)

查看详情

如何将 Curl api 示例写入 PHP curl

......请告诉我,如何理解CurlAPI示例并将其写入PHPcurl这里是简单的“appteak.com”curl示例curl-Ghttps://api.app 查看详情

php简单的ms-sql示例(代码片段)

查看详情

php使用curl和php(加上重定向)使用测量协议进行简单事件跟踪(代码片段)

查看详情

php利用curl实现多进程下载文件类(代码片段)

阅读目录场景示例场景批量下载文件一般使用循环的方式,逐一执行下载。但在带宽与服务器性能允许的情况下,使用多进程进行下载可以大大提高下载的效率。本文介绍PHP利用curl的多进程请求方法,实现多进程同时下载文件... 查看详情

php中curl详解(代码片段)

...且curl可以支持https认证、httppost、ftp上传、代理、cookies、简单口令认证等等功能啦例子<?php//创建curl会话资源$ch=curl_init();//设置urlcurl_setopt($ch,CURLOPT_URL,"baidu.com");//这是设置是 查看详情

sh如何通过curl使用主机工厂令牌创建主机的示例(代码片段)

查看详情

php完美的curl功能(代码片段)

查看详情

php来自php的httppost,没有curl(代码片段)

查看详情

php下载网络图片常用的三个方法总结(代码片段)

... 一起来看看吧,实际工作中经常用到。方法一优点:简单。缺点:效率相对较低。该方法主要用到的函数有两个file_get_contents以及file_put_contents这种方法非常简单,通俗的讲就是先get到图片内容,然后put到文件中。代码如下... 查看详情

php使用curl的基本php内容获取器(代码片段)

查看详情

php的curl功能扩展基本用法(代码片段)

阅读目录阐述curlget和post请求的方法get请求POST请求阐述PHP的curl功能提供了很多函数,需要将这些函数按特定的步骤组合到一起,我们先来了解下PHP建立curl请求的基本步骤。1$ch=curl_init();//创建一个新的CURL资源赋给变量$ch2curl_setop... 查看详情

php使用curl获取文件内容的函数。(代码片段)

查看详情

php查看文件是否存在curl的函数。(代码片段)

查看详情

phpcurl数据传输神器

...curl的使用场景三、在PHP中使用cUrl四、使用示例1、一个最简单的curl示例(编写一个网页爬虫,获取百度首页的html)2、对网页代码做一些修改(例如将百度改为慕课)3、访问WebService数据接口资源(查询深圳的天气)一、curl的概... 查看详情

phpcurl数据传输神器

...curl的使用场景三、在PHP中使用cUrl四、使用示例1、一个最简单的curl示例(编写一个网页爬虫,获取百度首页的html)2、对网页代码做一些修改(例如将百度改为慕课)3、访问WebService数据接口资源(查询深圳的天气)一、curl的概... 查看详情

使用curl进行模拟登录

...信息采集的时候,要采集的站点可能需要登录,这样使用简单的采集方式(例如file_get_contents)就无法做到了,我们可以利用PHP的CURL扩展库来进行模拟登录,下面给出代码示例:<?php$cookie_path=‘./‘;//设置cookie保存路径//-----登... 查看详情