Reading JSON POST using PHP

前端 未结 3 1074
予麋鹿
予麋鹿 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:45

    You have empty $_POST. If your web-server wants see data in json-format you need to read the raw input and then parse it with JSON decode.

    You need something like that:

    $json = file_get_contents('php://input');
    $obj = json_decode($json);
    

    Also you have wrong code for testing JSON-communication...

    CURLOPT_POSTFIELDS tells curl to encode your parameters as application/x-www-form-urlencoded. You need JSON-string here.

    UPDATE

    Your php code for test page should be like that:

    $data_string = json_encode($data);
    
    $ch = curl_init('http://webservice.local/');
    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 = curl_exec($ch);
    $result = json_decode($result);
    var_dump($result);
    

    Also on your web-service page you should remove one of the lines header('Content-type: application/json');. It must be called only once.

    0 讨论(0)
  • 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] . '</br>';
                //echo 'err: '.$err.'</br>';
                //echo 'errmsg: '.$errmsg.'</br>';
                //echo 'httpCode: '.$httpCode.'</br>';
                //print_r($header);
                //print_r(json_decode($content, true));
    
                if ($err == 0 && $httpCode == 200 && $header['download_content_length'] > 0) {
    
                    return json_decode($content, true);
    
                } 
    
            }
        }
    
    0 讨论(0)
  • 2020-11-22 02:50

    you can put your json in a parameter and send it instead of put only your json in header:

    $post_string= 'json_param=' . json_encode($data);
    
    //open connection
    $ch = curl_init();
    
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/');  // Set the url path we want to call
    
    //execute post
    $result = curl_exec($curl);
    
    //see the results
    $json=json_decode($result,true);
    curl_close($curl);
    print_r($json);
    

    on the service side you can get your json string as a parameter:

    $json_string = $_POST['json_param'];
    $obj = json_decode($json_string);
    

    then you can use your converted data as object.

    0 讨论(0)
提交回复
热议问题