php - laravel gets api data provided by others
大家讲道理
大家讲道理 2017-05-16 13:02:43
0
4
466

Someone gave me a url and the parameters are arrays of json type.
Do I need to request this url in laravel to get the data? ? ? How to implement the specific code

大家讲道理
大家讲道理

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

reply all(4)
世界只因有你

or

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

---------------------------------The following is another method--------- ----------------------------------

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;
    }
洪涛
<?php
header("Content-Type:text/html;charset=utf-8");
$url = "http://www.xxx.cc/xxx";
$params = [
    "xxx" => "xxxx",
    "xxxx" => "xxxx",
];

$data_string = json_encode($params);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);

$result = json_decode(curl_exec($ch), true);

curl_close($ch);

print_r($result);
黄舟

Get data from interface? ajax or curl

某草草

curl request interface to obtain data. You can use the guzzlehttp/guzzle package, which encapsulates the curl operation.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template