【问题标题】:How to send post json object in php?如何在 php 中发送 post json 对象?
【发布时间】:2017-09-30 07:38:55
【问题描述】:

我会在 php 中发送带有 post 的 Json 对象。如何通过 post 发送 json 对象。我在这里尝试了几个帖子,但它不起作用。

这是我尝试过的代码:

function get_user_post_other_count($user_id, $call_for)
{
    $params = array('user_id' => $user_id,
        'call_for' => $call_for);
// Build Http query using params
    $query = json_encode($params);

// Create Http context details
    $contextData = array(
        'method' => 'POST',
        'header' => 'Content-Type: application/json' . "\r\n" . 'Content-Length: ' . strlen($query) . "\r\n",
        'content' => $query);
    $context = stream_context_create(array('http' => $contextData));
    $result = file_get_contents(USER_PROFILE_COUNTER, false, $context);
    $json_decoded = json_decode($result, true);
    return $json_decoded;
}

【问题讨论】:

  • 向我们展示一些你尝试过的东西。也许它也能帮助我们了解你真正想要做什么
  • 我已经在上面编辑过了。你可以看看我的代码。任何帮助将不胜感激。谢谢

标签: php json post


【解决方案1】:

使用 json 和 ajax 将 javascript obj 发送到 php:

js:

var dataPost = {
   "var": "foo"
};
var dataString = JSON.stringify(dataPost);

$.ajax({
   url: 'server.php',
   data: {myData: dataString},
   type: 'POST',
   success: function(response) {
      alert(response);
   }
});

在 php 中使用该对象:

   $obj = json_decode($_POST["myData"]);

    echo $obj->var;

【讨论】:

    【解决方案2】:

    第 1 步:在您的 php 文件中添加此函数:

    <?php
    
    function do_post($url, $data, $optional_headers = null) {
    
      $params = ['http' => ['method' => 'POST', 'content' => $data]];
    
      if ($optional_headers !== null) {
          $params['http']['header'] = $optional_headers;
      }
    
      $ctx = stream_context_create($params);
      $fp = @fopen($url, 'rb', false, $ctx);
    
      if (!$fp) {
          throw new Exception("Exception caused with $url, $php_errormsg");
      }
    
      $response = @stream_get_contents($fp);
    
      if ($response === false) {
          throw new Exception("Error reading response from $url, $php_errormsg");
      }
    
      return $response;
    
    }
    

    第 2 步:相应地更改您的功能

    function get_user_post_other_count($user_id, $call_for) {
        $params = array('user_id' => $user_id, 'call_for' => $call_for);    
        $query = json_encode($params);
        $response = do_post($url, $query);
    
        return json_decode($response, true);
    }
    

    我会在这里推荐curl

    【讨论】:

      【解决方案3】:

      使用 PHP 卷曲 http://php.net/manual/en/curl.examples.php

      $ch = curl_init();

          // set url 
          curl_setopt($ch, CURLOPT_URL, "example.com"); 
      
          //return the transfer as a string 
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      
          // $output contains the output string 
          $output = curl_exec($ch); 
      
          // close curl resource to free up system resources 
          curl_close($ch);    
      

      【讨论】:

        【解决方案4】:
        $context_options = array (
                'http' => array (
                    'method' => 'POST',
                    'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
                        . "Content-Length: " . strlen($data) . "\r\n",
                    'content' => json_encode($data)
                    )
                );
        
        $fp = fopen('http://url', 'r', false, $context_options);
        

        【讨论】:

        • 我推荐使用 curl,因为它更可靠,但我想你提到避免 curl
        猜你喜欢
        • 1970-01-01
        • 2011-08-02
        • 2014-02-07
        • 2011-10-02
        • 2022-06-14
        • 2020-06-17
        • 2015-06-14
        • 2021-01-10
        • 1970-01-01
        相关资源
        最近更新 更多