php - laravel获取别人提供的api数据
大家讲道理
大家讲道理 2017-05-16 13:02:43
0
4
484

别人给我一个url 还有参数是json类型的数组
我要在laravel请求这个url得到数据???具体代码该怎么实现

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(4)
世界只因有你

或者

        $url = 'http://www.baidu.com/';
        $data['param1'] = '数组参数';
        $data['param2'] = '数组参数';
        $params=json_encode($data) ;
        $result = file_get_contents($url.'?param='.$params);

---------------------------------下边是另一种方法了---------------------------------------------

static function reqUrl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Data' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }
洪涛

雷雷

黄舟

从接口获取数据?ajax或者curl吧

某草草

curl请求接口获取数据。可以使用下guzzlehttp/guzzle这个包,里面封装了curl的操作。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板