<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>cURL | 酷 壳 - CoolShell</title>
	<atom:link href="https://coolshell.cn/tag/curl/feed" rel="self" type="application/rss+xml" />
	<link>https://coolshell.cn</link>
	<description>享受编程和技术所带来的快乐 - Coding Your Ambition</description>
	<lastBuildDate>Sat, 25 Apr 2009 09:18:12 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2</generator>
	<item>
		<title>使用PHP的cURL库</title>
		<link>https://coolshell.cn/articles/664.html</link>
					<comments>https://coolshell.cn/articles/664.html#comments</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Sat, 25 Apr 2009 09:12:32 +0000</pubDate>
				<category><![CDATA[PHP脚本]]></category>
		<category><![CDATA[编程语言]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=664</guid>

					<description><![CDATA[<p>使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本，然后分析一下你所抓取的网页，然后就可以以程序的方式得到你想要的数据了。无论是你想从从一个链...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/664.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/664.html">使用PHP的cURL库</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script><a href="https://coolshell.cn/wp-content/uploads/2009/04/phpinfo_curl.png"></a>使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本，然后分析一下你所抓取的网页，然后就可以以程序的方式得到你想要的数据了。无论是你想从从一个链接上取部分数据，或是取一个XML文件并把其导入数据库，那怕就是简单的获取网页内容，cURL 是一个功能强大的PHP库。本文主要讲述如果使用这个PHP库。</p>
<p><strong> 启用 cURL 设置</strong><br />
首先，我们得先要确定我们的PHP是否开启了这个库，你可以通过使用php_info()函数来得到这一信息。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">&lt;?php
    phpinfo();
?&gt;</pre>
<p><span id="more-664"></span></p>
<p> </p>
<p>如果你可以在网页上看到下面的输出，那么表示cURL库已被开启。</p>
<p><a href="https://coolshell.cn/wp-content/uploads/2009/04/phpinfo_curl.png"><img decoding="async" loading="lazy" title="phpinfo_curl" src="https://coolshell.cn/wp-content/uploads/2009/04/phpinfo_curl.png" alt="phpinfo_curl" width="498" height="91" /></a></p>
<p>如果你看到的话，那么你需要设置你的PHP并开启这个库。如果你是在Windows平台下，那么非常简单，你需要改一改你的php.ini文件的设置，找到php_curl.dll，并取消前面的分号注释就行了。如下所示：</p>
<pre data-enlighter-language="c" class="EnlighterJSRAW">

//取消下在的注释
extension=php_curl.dll 
</pre>
<p>如果你是在Linux下面，那么，你需要重新编译你的PHP了，编辑时，你需要打开编译参数——在configure命令上加上“&#8211;with-curl” 参数。</p>
<p><strong>一个小示例</strong><br />
如果一切就绪，下面是一个小例程：</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW">
&lt;?php
// 初始化一个 cURL 对象
$curl = curl_init(); 

// 设置你需要抓取的URL
curl_setopt($curl, CURLOPT_URL, &#039;https://coolshell.cn&#039;);

// 设置header
curl_setopt($curl, CURLOPT_HEADER, 1);

// 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// 运行cURL，请求网页
$data = curl_exec($curl);

// 关闭URL请求
curl_close($curl);

// 显示获得的数据
var_dump($data);
</pre>
<p> </p>
<p><strong>如何POST数据</strong></p>
<p>上面是抓取网页的代码，下面则是向某个网页POST数据。假设我们有一个处理表单的网址<a href="http://www.example.com/sendSMS.php">http://www.example.com/sendSMS.php</a>，其可以接受两个表单域，一个是电话号码，一个是短信内容。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW" data-enlighter-highlight="9,10">
&lt;?php
    $phoneNumber = &#039;13912345678&#039;;
    $message = &#039;This message was generated by curl and php&#039;;
    $curlPost = &#039;pNUMBER=&#039;  . urlencode($phoneNumber) . &#039;&amp;MESSAGE=&#039; . urlencode($message) . &#039;&amp;SUBMIT=Send&#039;;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, &#039;http://www.example.com/sendSMS.php&#039;);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec();
    curl_close($ch);
?&gt;
</pre>
<p> 从上面的程序我们可以看到，使用CURLOPT_POST设置HTTP协议的POST方法，而不是GET方法，然后以CURLOPT_POSTFIELDS设置POST的数据。</p>
<p><strong>关于代理服务器</strong></p>
<pre>下面是一个如何使用代理服务器的示例。请注意其中高亮的代码，代码很简单，我就不用多说了。</pre>
<pre data-enlighter-language="php" class="EnlighterJSRAW" data-enlighter-highlight="6,7,8">
&lt;?php 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, &#039;http://www.example.com&#039;);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    curl_setopt($ch, CURLOPT_PROXY, &#039;fakeproxy.com:1080&#039;);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, &#039;user:password&#039;);
    $data = curl_exec();
    curl_close($ch);
?&gt;

</pre>
<p> <strong>关于SSL和Cookie</strong></p>
<p>关于SSL也就是HTTPS协议，你只需要把CURLOPT_URL连接中的http://变成https://就可以了。当然，还有一个参数叫CURLOPT_SSL_VERIFYHOST可以设置为验证站点。</p>
<p>关于Cookie，你需要了解下面三个参数：</p>
<ul>
<li>CURLOPT_COOKIE，在当面的会话中设置一个cookie</li>
<li>CURLOPT_COOKIEJAR，当会话结束的时候保存一个Cookie</li>
<li>CURLOPT_COOKIEFILE，Cookie的文件。</li>
</ul>
<p><strong>HTTP服务器认证</strong></p>
<p>最后，我们来看一看HTTP服务器认证的情况。</p>
<pre data-enlighter-language="php" class="EnlighterJSRAW" data-enlighter-highlight="5,6">
&lt;?php 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, &#039;http://www.example.com&#039;);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt(CURLOPT_USERPWD, &#039;[username]:[password]&#039;)

    $data = curl_exec();
    curl_close($ch);
?&gt;
</pre>
<p>关于其它更多的内容，请大家参看相关的cURL手册吧。<!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" id="wp_rp_first"><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/07/muxnt-150x150.jpg" alt="代码执行的效率" width="150" height="150" /></a><a href="https://coolshell.cn/articles/7886.html" class="wp_rp_title">代码执行的效率</a></li><li ><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/09/image008-150x150.jpg" alt="一些文章和各种资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5224.html" class="wp_rp_title">一些文章和各种资源</a></li><li ><a href="https://coolshell.cn/articles/5160.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/08/Pagination-e1312791884744-150x150.jpg" alt="PHP分页技术的代码和示例" width="150" height="150" /></a><a href="https://coolshell.cn/articles/5160.html" class="wp_rp_title">PHP分页技术的代码和示例</a></li><li ><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/7.jpg" alt="开源中最好的Web开发的资源" width="150" height="150" /></a><a href="https://coolshell.cn/articles/4795.html" class="wp_rp_title">开源中最好的Web开发的资源</a></li><li ><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2011/02/1128-150x150.jpg" alt="Web开发人员速查卡" width="150" height="150" /></a><a href="https://coolshell.cn/articles/3684.html" class="wp_rp_title">Web开发人员速查卡</a></li><li ><a href="https://coolshell.cn/articles/2394.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/1.jpg" alt="九个PHP很有用的功能" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2394.html" class="wp_rp_title">九个PHP很有用的功能</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/664.html">使用PHP的cURL库</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/664.html/feed</wfw:commentRss>
			<slash:comments>12</slash:comments>
		
		
			</item>
	</channel>
</rss>
