【问题标题】:Returning JSON from a PHP Script从 PHP 脚本返回 JSON
【发布时间】:2015-10-18 18:16:34
【问题描述】:

我想从 PHP 脚本返回 JSON。

我只是回显结果吗?我必须设置Content-Type 标头吗?

【问题讨论】:

    标签: php json header


    【解决方案1】:

    虽然没有它你通常没问题,但你可以并且应该设置 Content-Type 标头:

    <?php
    $data = /** whatever you're serializing **/;
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data);
    

    如果我没有使用特定的框架,我通常会允许一些请求参数来修改输出行为。通常对于快速故障排除而言,不发送标头或有时print_r 数据有效负载以观察它可能很有用(尽管在大多数情况下,它不应该是必需的)。

    【讨论】:

    • 以防万一:除了输出缓冲之外,您应该只使用 header() 命令以避免“标头已发送”警告
    • header('Content-type:application/json;charset=utf-8');
    • @mikepote 实际上我认为没有必要在 PHP 文件的顶部放置 header 命令。如果您无意中吐出一些东西而导致您的标头命令出错,那么您只需要修复您的代码,因为它已损坏。
    • @KrzysztofKalinowski 不,PHP 文件不需要进行 UTF-8 编码。输出必须是 UTF-8 编码的。那些错误的陈述并不能帮助没有经验的用户学习如何避免事情被破坏,但它有助于增加他们的神话,并且永远不会知道编码在流中扮演什么角色以及它们是如何工作的。
    • @timdev 不要忘记在echo json_encode($data); 之后立即调用die(); 中的exit();,否则脚本中的随机数据(例如分析)可能会附加到您的json 响应中。
    【解决方案2】:

    如果您在 WordPress 中执行此操作,那么有一个简单的解决方案:

    add_action( 'parse_request', function ($wp) {
        $data = /* Your data to serialise. */
        wp_send_json_success($data); /* Returns the data with a success flag. */
        exit(); /* Prevents more response from the server. */
    })
    

    请注意,这不在wp_head 挂钩中,即使您立即退出,它也会始终返回大部分头部。 parse_request 在序列中出现得更早。

    【讨论】:

      【解决方案3】:

      这个问题有很多答案,但没有一个涵盖返回干净 JSON 的整个过程,以及防止 JSON 响应格式错误所需的一切。

      
      /*
       * returnJsonHttpResponse
       * @param $success: Boolean
       * @param $data: Object or Array
       */
      function returnJsonHttpResponse($success, $data)
      {
          // remove any string that could create an invalid JSON 
          // such as PHP Notice, Warning, logs...
          ob_clean();
      
          // this will clean up any previously added headers, to start clean
          header_remove(); 
      
          // Set the content type to JSON and charset 
          // (charset can be set to something else)
          header("Content-type: application/json; charset=utf-8");
      
          // Set your HTTP response code, 2xx = SUCCESS, 
          // anything else will be error, refer to HTTP documentation
          if ($success) {
              http_response_code(200);
          } else {
              http_response_code(500);
          }
          
          // encode your PHP Object or Array into a JSON string.
          // stdClass or array
          echo json_encode($data);
      
          // making sure nothing is added
          exit();
      }
      
      

      参考资料:

      response_remove

      ob_clean

      Content-type JSON

      HTTP Codes

      http_response_code

      json_encode

      【讨论】:

      • 请参阅 ob_clean 参考。我有一条领先的线正在提升我的 fetch response.json() 调用。
      【解决方案4】:

      一个简单的函数,用于返回带有 HTTP 状态代码JSON 响应

      function json_response($data=null, $httpStatus=200)
      {
          header_remove();
      
          header("Content-Type: application/json");
      
          http_response_code($httpStatus);
      
          echo json_encode($data);
      
          exit();
      }
      

      【讨论】:

      • header_remove,明确设置http响应是个好主意;虽然设置状态然后 http_response 似乎是多余的。可能还想在末尾添加 exit 语句。我将您的功能与@trincot 的功能结合在一起:stackoverflow.com/a/35391449/339440
      • 使用 JS FetchAPI,您能否扩展您对如何接收发送数据的回答? fetch(...).then(res =&gt; res.json()).then(data =&gt; /* do smth */).catch(e =&gt; console.error(e)) 在响应为200 时效果很好,但是如何在500 上获取$data 以在JS 中的.catch() 方法中显示PHP 中抛出的确切错误?
      • 要实现这一点,您必须将代码包装在 try catch 中:try { /* code... */ json_response('Success!', 200); } catch (\Exception $e) { json_response($e-&gt;getMessage(), 500); }
      【解决方案5】:

      根据manual on json_encode方法可以返回一个非字符串(false):

      成功时返回 JSON 编码字符串,失败时返回 FALSE

      当这种情况发生时,echo json_encode($data) 将输出空字符串,即invalid JSON

      json_encode 例如,如果其参数包含非 UTF-8 字符串,则将失败(并返回 false)。

      这种错误情况应该在 PHP 中被捕获,例如:

      <?php
      header("Content-Type: application/json");
      
      // Collect what you need in the $data variable.
      
      $json = json_encode($data);
      if ($json === false) {
          // Avoid echo of empty string (which is invalid JSON), and
          // JSONify the error message instead:
          $json = json_encode(["jsonError" => json_last_error_msg()]);
          if ($json === false) {
              // This should not happen, but we go all the way now:
              $json = '{"jsonError":"unknown"}';
          }
          // Set HTTP response status code to: 500 - Internal Server Error
          http_response_code(500);
      }
      echo $json;
      ?>
      

      那么接收端当然应该知道 jsonError 属性的存在表示错误情况,它应该相应地处理。

      在生产模式下,最好只向客户端发送一般错误状态并记录更具体的错误消息以供以后调查。

      PHP's Documentation 中阅读有关处理 JSON 错误的更多信息。

      【讨论】:

      • JSON 没有 charset 参数;请参阅tools.ietf.org/html/rfc8259#section-11 末尾的注释:“没有为此注册定义'charset'参数。添加一个对合规收件人确实没有影响。” (根据tools.ietf.org/html/rfc8259#section-8.1,JSON 必须以 UTF-8 传输,因此指定它被编码为 UTF-8 有点多余。)
      • 感谢您强调这一点,@PatrickDark。从 HTTP 标头字符串中删除了冗余的 charset 参数。
      【解决方案6】:

      无论何时您尝试为 API 返回 JSON 响应,或者确保您有正确的标头并确保您返回有效的 JSON 数据。

      这是帮助您从 PHP 数组或返回 JSON 响应的示例脚本 来自 JSON 文件。

      PHP 脚本(代码):

      <?php
      
      // Set required headers
      header('Content-Type: application/json; charset=utf-8');
      header('Access-Control-Allow-Origin: *');
      
      /**
       * Example: First
       *
       * Get JSON data from JSON file and retun as JSON response
       */
      
      // Get JSON data from JSON file
      $json = file_get_contents('response.json');
      
      // Output, response
      echo $json;
      
      /** =. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =.  */
      
      /**
       * Example: Second
       *
       * Build JSON data from PHP array and retun as JSON response
       */
      
      // Or build JSON data from array (PHP)
      $json_var = [
        'hashtag' => 'HealthMatters',
        'id' => '072b3d65-9168-49fd-a1c1-a4700fc017e0',
        'sentiment' => [
          'negative' => 44,
          'positive' => 56,
        ],
        'total' => '3400',
        'users' => [
          [
            'profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg',
            'screen_name' => 'rayalrumbel',
            'text' => 'Tweet (A), #HealthMatters because life is cool :) We love this life and want to spend more.',
            'timestamp' => '{{$timestamp}}',
          ],
          [
            'profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg',
            'screen_name' => 'mikedingdong',
            'text' => 'Tweet (B), #HealthMatters because life is cool :) We love this life and want to spend more.',
            'timestamp' => '{{$timestamp}}',
          ],
          [
            'profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg',
            'screen_name' => 'ScottMili',
            'text' => 'Tweet (C), #HealthMatters because life is cool :) We love this life and want to spend more.',
            'timestamp' => '{{$timestamp}}',
          ],
          [
            'profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg',
            'screen_name' => 'yogibawa',
            'text' => 'Tweet (D), #HealthMatters because life is cool :) We love this life and want to spend more.',
            'timestamp' => '{{$timestamp}}',
          ],
        ],
      ];
      
      // Output, response
      echo json_encode($json_var);
      

      JSON 文件(JSON 数据):

      {
          "hashtag": "HealthMatters", 
          "id": "072b3d65-9168-49fd-a1c1-a4700fc017e0", 
          "sentiment": {
              "negative": 44, 
              "positive": 56
          }, 
          "total": "3400", 
          "users": [
              {
                  "profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg", 
                  "screen_name": "rayalrumbel", 
                  "text": "Tweet (A), #HealthMatters because life is cool :) We love this life and want to spend more.", 
                  "timestamp": "{{$timestamp}}"
              }, 
              {
                  "profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg", 
                  "screen_name": "mikedingdong", 
                  "text": "Tweet (B), #HealthMatters because life is cool :) We love this life and want to spend more.", 
                  "timestamp": "{{$timestamp}}"
              }, 
              {
                  "profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg", 
                  "screen_name": "ScottMili", 
                  "text": "Tweet (C), #HealthMatters because life is cool :) We love this life and want to spend more.", 
                  "timestamp": "{{$timestamp}}"
              }, 
              {
                  "profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg", 
                  "screen_name": "yogibawa", 
                  "text": "Tweet (D), #HealthMatters because life is cool :) We love this life and want to spend more.", 
                  "timestamp": "{{$timestamp}}"
              }
          ]
      }
      
      

      JSON 截图:

      【讨论】:

        【解决方案7】:

        这是一个简单的 PHP 脚本,用于返回男性女性和用户 ID,因为 json 值将是您调用脚本 json.php 时的任何随机值。

        希望这个帮助谢谢

        <?php
        header("Content-type: application/json");
        $myObj=new \stdClass();
        $myObj->user_id = rand(0, 10);
        $myObj->male = rand(0, 5);
        $myObj->female = rand(0, 5);
        $myJSON = json_encode($myObj);
        echo $myJSON;
        ?>
        

        【讨论】:

        • JSON 文本的 MIME 媒体类型是 application/json
        【解决方案8】:

        尝试json_encode 对数据进行编码,并将内容类型设置为header('Content-type: application/json');

        【讨论】:

          【解决方案9】:
          <?php
          $data = /** whatever you're serializing **/;
          header("Content-type: application/json; charset=utf-8");
          echo json_encode($data);
          ?>
          

          【讨论】:

          • 在标题中说明字符集有什么区别?请解释一下,谢谢。
          【解决方案10】:

          将域对象格式化为 JSON 的一种简单方法是使用 Marshal Serializer。 然后将数据传递给json_encode 并根据您的需要发送正确的 Content-Type 标头。 如果您使用的是 Symfony 之类的框架,则无需手动设置标头。在那里你可以使用JsonResponse

          例如,处理 Javascript 的正确 Content-Type 应该是 application/javascript

          或者,如果您需要支持一些相当旧的浏览器,最安全的是text/javascript

          对于所有其他用途,例如移动应用,请使用 application/json 作为 Content-Type。

          这是一个小例子:

          <?php
          ...
          $userCollection = [$user1, $user2, $user3];
          
          $data = Marshal::serializeCollectionCallable(function (User $user) {
              return [
                  'username' => $user->getUsername(),
                  'email'    => $user->getEmail(),
                  'birthday' => $user->getBirthday()->format('Y-m-d'),
                  'followers => count($user->getFollowers()),
              ];
          }, $userCollection);
          
          header('Content-Type: application/json');
          echo json_encode($data);
          

          【讨论】:

            【解决方案11】:

            如果您查询数据库并需要 JSON 格式的结果集,可以这样做:

            <?php
            
            $db = mysqli_connect("localhost","root","","mylogs");
            //MSG
            $query = "SELECT * FROM logs LIMIT 20";
            $result = mysqli_query($db, $query);
            //Add all records to an array
            $rows = array();
            while($row = $result->fetch_array()){
                $rows[] = $row;
            }
            //Return result to jTable
            $qryResult = array();
            $qryResult['logs'] = $rows;
            echo json_encode($qryResult);
            
            mysqli_close($db);
            
            ?>
            

            有关使用 jQuery 解析结果的帮助,请查看 this tutorial

            【讨论】:

              【解决方案12】:

              设置访问安全性也很好 - 只需将 * 替换为您希望能够访问它的域。

              <?php
              header('Access-Control-Allow-Origin: *');
              header('Content-type: application/json');
                  $response = array();
                  $response[0] = array(
                      'id' => '1',
                      'value1'=> 'value1',
                      'value2'=> 'value2'
                  );
              
              echo json_encode($response); 
              ?>
              

              这里有更多示例:how to bypass Access-Control-Allow-Origin?

              【讨论】:

              • 如果这不起作用是什么意思?例如,为了只限制来自 CodePen 的调用,我尝试了header('Access-Control-Allow-Origin: https://cdpn.io');,但我仍然可以从自己的浏览器加载页面。
              • 它用于阻止跨脚本(一个脚本调用另一个页面)。因此,您将能够直接从浏览器加载它,但不能使用脚本从另一个域加载它。
              【解决方案13】:

              如上所述:

              header('Content-Type: application/json');
              

              将完成这项工作。但请记住:

              • 即使不使用此标头,Ajax 读取 json 也没有问题,除非您的 json 包含一些 HTML 标签。在这种情况下,您需要将标头设置为 application/json。

              • 确保您的文件未以 UTF8-BOM 编码。这种格式会在文件顶部添加一个字符,因此您的 header() 调用将失败。

              【讨论】:

                【解决方案14】:

                返回 JSON 的完整清晰的 PHP 代码是:

                $option = $_GET['option'];
                
                if ( $option == 1 ) {
                    $data = [ 'a', 'b', 'c' ];
                    // will encode to JSON array: ["a","b","c"]
                    // accessed as example in JavaScript like: result[1] (returns "b")
                } else {
                    $data = [ 'name' => 'God', 'age' => -1 ];
                    // will encode to JSON object: {"name":"God","age":-1}  
                    // accessed as example in JavaScript like: result.name or result['name'] (returns "God")
                }
                
                header('Content-type: application/json');
                echo json_encode( $data );
                

                【讨论】:

                • 你应该事先做:$option = isset($_GET['option']);
                【解决方案15】:

                如果您需要从发送自定义信息的 php 获取 json,您可以在打印任何其他内容之前添加此 header('Content-Type: application/json');,然后您可以打印您的自定义 echo '{"monto": "'.$monto[0]-&gt;valor.'","moneda":"'.$moneda[0]-&gt;nombre.'","simbolo":"'.$moneda[0]-&gt;simbolo.'"}';

                【讨论】:

                  【解决方案16】:

                  您可以使用此little PHP library。它发送标头并为您提供一个对象以轻松使用它。

                  看起来像:

                  <?php
                  // Include the json class
                  include('includes/json.php');
                  
                  // Then create the PHP-Json Object to suits your needs
                  
                  // Set a variable ; var name = {}
                  $Json = new json('var', 'name'); 
                  // Fire a callback ; callback({});
                  $Json = new json('callback', 'name'); 
                  // Just send a raw JSON ; {}
                  $Json = new json();
                  
                  // Build data
                  $object = new stdClass();
                  $object->test = 'OK';
                  $arraytest = array('1','2','3');
                  $jsonOnly = '{"Hello" : "darling"}';
                  
                  // Add some content
                  $Json->add('width', '565px');
                  $Json->add('You are logged IN');
                  $Json->add('An_Object', $object);
                  $Json->add("An_Array",$arraytest);
                  $Json->add("A_Json",$jsonOnly);
                  
                  // Finally, send the JSON.
                  
                  $Json->send();
                  ?>
                  

                  【讨论】:

                    【解决方案17】:

                    是的,您需要使用 echo 来显示输出。模仿类型:application/json

                    【讨论】:

                      【解决方案18】:

                      你的问题的答案is here

                      它说。

                      JSON 文本的 MIME 媒体类型是 应用程序/json。

                      因此,如果您将标头设置为该类型,并输出您的 JSON 字符串,它应该可以工作。

                      【讨论】:

                        【解决方案19】:

                        使用header('Content-type: application/json'); 设置内容类型,然后回显您的数据。

                        【讨论】:

                          猜你喜欢
                          • 2011-01-06
                          • 2023-03-20
                          • 1970-01-01
                          • 1970-01-01
                          • 2011-04-15
                          相关资源
                          最近更新 更多