cari

Rumah  >  Soal Jawab  >  teks badan

php - laravel mendapat data api yang disediakan oleh orang lain

Seseorang memberi saya url dan parameternya ialah tatasusunan jenis json
Adakah saya perlu meminta url ini dalam laravel untuk mendapatkan data? ? ? Cara melaksanakan kod khusus

大家讲道理大家讲道理2830 hari yang lalu490

membalas semua(4)saya akan balas

  • 世界只因有你

    世界只因有你2017-05-16 13:04:43

    atau

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

    --------------------------------Berikut adalah kaedah lain--------- -- ---------------------------------

    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;
        }

    balas
    0
  • PHP中文网

    PHP中文网2017-05-16 13:04:43

    <?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);

    balas
    0
  • 黄舟

    黄舟2017-05-16 13:04:43

    Dapatkan data daripada antara muka? ajax atau curl

    balas
    0
  • 某草草

    某草草2017-05-16 13:04:43

    antara muka permintaan curl untuk mendapatkan data. Anda boleh menggunakan pakej guzzlehttp/guzzle, yang merangkumi operasi curl.

    balas
    0
  • Batalbalas