Reading JSON POST using PHP

前端 未结 3 1081
予麋鹿
予麋鹿 2020-11-22 02:18

I looked around a lot before posting this question so my apologies if it is on another post and this is only my second quesiton on here so apologies if I don\'t format this

3条回答
  •  梦谈多话
    2020-11-22 02:48

    Hello this is a snippet from an old project of mine that uses curl to get ip information from some free ip databases services which reply in json format. I think it might help you.

    $ip_srv = array("http://freegeoip.net/json/$this->ip","http://smart-ip.net/geoip-json/$this->ip");
    
    getUserLocation($ip_srv);
    

    Function:

    function getUserLocation($services) {
    
            $ctx = stream_context_create(array('http' => array('timeout' => 15))); // 15 seconds timeout
    
            for ($i = 0; $i < count($services); $i++) {
    
                // Configuring curl options
                $options = array (
                    CURLOPT_RETURNTRANSFER => true, // return web page
                    //CURLOPT_HEADER => false, // don't return headers
                    CURLOPT_HTTPHEADER => array('Content-type: application/json'),
                    CURLOPT_FOLLOWLOCATION => true, // follow redirects
                    CURLOPT_ENCODING => "", // handle compressed
                    CURLOPT_USERAGENT => "test", // who am i
                    CURLOPT_AUTOREFERER => true, // set referer on redirect
                    CURLOPT_CONNECTTIMEOUT => 5, // timeout on connect
                    CURLOPT_TIMEOUT => 5, // timeout on response
                    CURLOPT_MAXREDIRS => 10 // stop after 10 redirects
                ); 
    
                // Initializing curl
                $ch = curl_init($services[$i]);
                curl_setopt_array ( $ch, $options );
    
                $content = curl_exec ( $ch );
                $err = curl_errno ( $ch );
                $errmsg = curl_error ( $ch );
                $header = curl_getinfo ( $ch );
                $httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
    
                curl_close ( $ch );
    
                //echo 'service: ' . $services[$i] . '
    '; //echo 'err: '.$err.'
    '; //echo 'errmsg: '.$errmsg.'
    '; //echo 'httpCode: '.$httpCode.'
    '; //print_r($header); //print_r(json_decode($content, true)); if ($err == 0 && $httpCode == 200 && $header['download_content_length'] > 0) { return json_decode($content, true); } } }

提交回复
热议问题