Reading JSON POST using PHP

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

提交回复
热议问题