zoukankan      html  css  js  c++  java
  • PHP与cURL

    CURL PHP API comes with a wide array of options and features. This allows users to fine tune the requests and the way that the responses are handled.

    PHP CURL API带来广阔的选择和特性,帮助用户处理请求和其他信息。

    步骤概览


    初始化CURL->设置选项->执行CURL->关闭CURL

    Initialize

    $ch = curl_init();
    

    Set Options

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_URL, true);
    

    Execute

    curl_exec($ch);
    

    Close

    curl_close($ch);
    

    与file_get_contents

    Earlier in this chapter it was shown how to access the Yahoo spelling service with
    the file_get_contents function. The following code shows how to do the same with CURL. As you will notice, the code is a bit lengthier than the equivalent
    file_get_contents version. Obviously, this is the cost you have to pay in exchange of the customizability of CURL. However, you will soon realize that the increased number of lines is negligible in comparison to what you can do with CURL.

    与file_get_contents相比,在实现同样功能的前提下,CURL的代码量会更长。这是我们复杂化处理数据需要付出的代价。然而你讲很快发现增加的代码量相对于你的工作而言,根本无足轻重。

    <?php
    /*//yahoo weather web service
    $url='http://api.wooyun.org/domain/cnpc.com';
    $xml = file_get_contents($url);
    $out=json_decode($xml);
    print_r($out);*/
    $url='http://api.wooyun.org/domain/cnpc.com';
    
    $ch=curl_init();
    
    curl_setopt($ch,CURLOPT_URL,$url);
    //能够接受返回值
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    
    $res=json_decode(curl_exec($ch));
    curl_close($ch);
    print_r($res);
    ?>
    

    这是我的个人日记本
  • 相关阅读:
    centos7之添加开机启动服务/脚本
    高性能 Windows C++ 通用组件 VCLogger v2.0.3 正式发布
    Kerberos认证流程
    使用Spring.Net+NHibernate构建WCF应用
    国家重点基础研究发展计划和重大科学研究计划方向
    项目实现思路(不断更新)
    LINQ新特性简介及入门教程
    XXX公司CRM项目开发日志
    GridView的增删改查和分页
    团队开发之环境搭建
  • 原文地址:https://www.cnblogs.com/valentineisme/p/4166663.html
Copyright © 2011-2022 走看看