PHP下使用CURL方式POST数据至API接口的代码

yipeiwu_com6年前PHP代码库

其实,也比较简单,上代码:

复制代码 代码如下:

<?php     

    $url = 'http://127.0.0.1/test.php';//POST指向的链接     
    $data = array(     
        'access_token'=>'thekeyvalue'    
    );     

    $json_data = postData($url, $data);     
    $array = json_decode($json_data,true);     
    echo '<pre>';print_r($array);     

    function postData($url, $data)     
    {     
        $ch = curl_init();     
        $timeout = 300;      
        curl_setopt($ch, CURLOPT_URL, $url);    
        curl_setopt($ch, CURLOPT_REFERER, "//www.jb51.net/");   //构造来路   
        curl_setopt($ch, CURLOPT_POST, true);     
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);     
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);     
        $handles = curl_exec($ch);     
        curl_close($ch);     
        return $handles;     
    }     
?>

相关文章

PHP正则匹配反斜杠'\'和美元'$'的方法

本文实例讲述了PHP正则匹配反斜杠'\'和美元'$'的方法。分享给大家供大家参考,具体如下: 1. test.php: <?php $content = '111111...

PHPUnit PHP测试框架安装方法

单元测试是几个现代敏捷开发方法的基础,使得PHPUnit成为许多大型PHP项目的关键工具。这个工具也可以被Xdebug扩展用来生成代码覆盖率报告 ,并且可以与phing集成来自动测试,最...

php中怎么搜索相关联数组键值及获取之

1.搜索关联数组键如果在一个数组中找到一个指定的键,函数array_key_exists()返回TRUE,否则返回FALSE。其 形式如下:boolean array_key_exist...

php使用GD创建保持宽高比缩略图的方法

本文实例讲述了php使用GD创建保持宽高比缩略图的方法。分享给大家供大家参考。具体如下: /** * Create a thumbnail image from $inputFile...

PHP自定义函数获取搜索引擎来源关键字的方法

本文实例讲述了PHP自定义函数获取搜索引擎来源关键字的方法。分享给大家供大家参考,具体如下: 获取搜索引擎来源关键字的函数: function getKeywords() { /...