php curl 网页内容


PHP Curl是一个用于读取Web页面以及通过Web页面与其他服务进行通信的PHP扩展程序。使用PHP Curl可以轻松地访问网站的内容,并从其中提取数据。

例如,如果你希望获取Google搜索的结果,你可以使用PHP Curl来检索Google搜索页面的内容。下面是使用PHP Curl下载Google搜索网页的示例代码:

$url = 'http://www.google.com/search?q=PHP+Curl';$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$result = curl_exec($ch);curl_close($ch);

上面的代码中,我指定了要查询的Google搜索网址,并使用curl_init()函数创建一个新的Curl资源。然后,我使用curl_setopt()函数设置这个Curl资源的选项,例如要获取的URL和是否将结果作为字符串返回。最后,我使用curl_exec()执行这个Curl资源并关闭它。

当运行这个脚本时,$result变量将包含Google搜索页面的内容。你可以进行字符串操作来提取您需要的信息,例如获取搜索结果的标题:

preg_match('/(.*?)<\/title>/', $result, $matches);$title = $matches[1];echo $title;</pre><p>在上面的代码中,我使用正则表达式从搜索页面中提取标题。该正则表达式匹配标题元素的开始和结束标记之间的所有内容,并将其保存在$matches数组的第二个项中。我可以使用$matches[1]读取此值并将其作为标题输出。</p><p>除了请求Web页面之外,PHP Curl还可以用于向Web API发送请求和从中响应。例如,您可以使用PHP Curl向Twitter发送GET请求来检索新的推文,如下所示:</p><pre>$url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitterapi&count=2';$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$result = curl_exec($ch);curl_close($ch);</pre><p>在上面的代码中,我指定了Twitter API的URL和请求参数,以获取用户twitterapi的最新两个状态。通过设置CURLOPT_RETURNTRANSFER选项,我告诉PHP Curl将结果返回为字符串。最后,我使用curl_exec()执行Curl请求。</p><p>当运行此脚本时,$result变量将包含请求返回的JSON字符串,其中包含用户的两个最新状态。</p><p>总结起来,PHP Curl是一个非常有用的PHP扩展程序,可以用于读取Web页面内容,向Web API发送请求和响应,以及与其他服务进行通信。下次您需要读取Web页面或向Web API发送请求时,请考虑使用PHP Curl。</p><br>
<p>上一篇:<a href='http://www.inhv.cn/wlzs/215833.html'>php curl 用法</a> </p><p>下一篇:<a href='http://www.inhv.cn/wlzs/215835.html'>php curl 类</a> </p><hr>
        
<a href='http://www.inhv.cn/tags/157.html'>php</a> 
            
<a href='http://www.inhv.cn/tags/1214.html'>curl</a> 
            
<a href='http://www.inhv.cn/tags/1507.html'>网页内容</a> 
            

</div>


<div class="showmore-btn" id="showmore-btn"></div>
    </div> 
    <div class="kuang show" style="margin-bottom:8px;">
<a  href="http://www.inhv.cn/wlzs/121670.html">南阳代办公司工商营业执照</a>
<a  href="http://www.inhv.cn/wlzs/121664.html">南阳代办营业执照工商执照公司</a>
<a  href="http://www.inhv.cn/wlzs/121665.html">南阳宛城区代办申请工商营业执照</a>
<a  href="http://www.inhv.cn/wlzs/121666.html">南阳卧龙区代办工商营业执照</a>
<a  href="http://www.inhv.cn/wlzs/121667.html">南阳个体户营业执照代办</a>
<a  href="http://www.inhv.cn/wlzs/121668.html">南阳公司工商营业执照代办</a>
<a  href="http://www.inhv.cn/wlzs/121669.html">南阳代办公司工商营业执照细节流</a>
<a  href="http://www.inhv.cn/wlzs/121663.html">南阳代办营业执照需要的资料</a>
<a  href="http://www.inhv.cn/wlzs/121662.html">唐山增值电信业务经营许可证申请</a>
<a  href="http://www.inhv.cn/wlzs/121661.html">廊坊第二类增值电信业,ICP许可证</a>
</div> 
 <div class="kuang show" style="margin-bottom:8px;"></div> 
  <div class="sjshow" style="margin-top:4px;clear:both;">
</div>
   </div> 
   <!-- 左侧结束 --> 
   <!-- 右侧开始 --> 
   <div id="main_right" class="show"> 
    <div id="main_right_zi"><div class='show' style='width:300px;height:250px;background-color:#DDF4FE;margin-bottom:8px;'>
</div>
    <div class='show' style='background-color:#DDF4FE;height:250px;margin-bottom:8px;'></div><div>
	</div> 
    </div> 
   </div> 
   <!-- 右侧结束 --> 
 
  </div> 

  <div id="bottom"><form  name="formsearch" action="/a/search.php">
        <div class="form">
           <input type="hidden" name="kwtype" value="0" />
           <input name="q" type="text" class="search-keyword" id="search-keyword" value="在这里输入关键词搜索..." onfocus="if(this.value=='在这里输入关键词搜索...'){this.value='';}"  onblur="if(this.value==''){this.value='在这里输入关键词搜索...';}" />
           <select name="searchtype" class="search-option" id="search-option">
               <option value="title" selected='1'>检索标题</option>
           </select>
          <button type="submit" class="search-submit">搜索</button>
        </div>
        </form>
Copyright © 2002-2019 <a href="//www.inhv.cn/" >测速网</a> https://www.inhv.cn/  <a href="http://beian.miit.gov.cn/" target="_blank">皖ICP备2023010105号</a> <a href="//www.inhv.cn/dxcity.php" >城市</a> <a href="//www.inhv.cn/dxdiqu.php" >地区</a> <a href="//www.inhv.cn/dxjiedao.php" >街道</a><br>温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!<div style="display:none"><script type="text/javascript" src="https://www.inhv.cn//ad/tj.js"></script></div> <a href="http://www.inhv.cn/tags.php">热门搜索</a>
  </div>
    <div style="padding:10px"><div style="display:none"></div></div><script language="JavaScript">
document.oncontextmenu=new Function("event.returnValue=false;");
document.onselectstart=new Function("event.returnValue=false;");
</script> 
 </body>
</html>