PHP form提交到某API地址后,接收返回的json的问题
PHP的form表单提交后,跳转到新页面,新页面中显示返回的JSON数据。怎么把这个返回的JSON数据保存下来?返回的JSON数据是否可以获取下来,以便处理。只要其中"retData"值
回复内容:
PHP的form表单提交后,跳转到新页面,新页面中显示返回的JSON数据。怎么把这个返回的JSON数据保存下来?返回的JSON数据是否可以获取下来,以便处理。只要其中"retData"值
看题主应该是比较新的新手吧。我猜那个API应该不是你自己的吧,按照楼上的盆友的说法,如果你在前端进行提交的话,用ajax把数据post到对应的API就可以获取相应的结果了。或者你可以自己做个PHP代理,把数据提交到自己创建的PHP,然后在PHP里面使用curl真正把数据post到对应的API,在获取相应的结果后,要怎么处理就随你了。
贴点代码给你吧
<code>function httpPost($url,$params) { $postData = ''; //create name value pairs seperated by & foreach($params as $k => $v) { $postData .= $k . '='.$v.'&'; } $postData = rtrim($postData, '&'); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_POST, count($postData)); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); $output=curl_exec($ch); curl_close($ch); return $output; }</code>
这样用
<code>$params = array( "name" => "Ravishanker Kusuma", "age" => "32", "location" => "India" ); echo httpPost("http://hayageek.com/examples/php/curl-examples/post.php",$params);</code>
代码摘自这里
可以啊. 不知道你这里是要前端存还是后端存?
表单的话要你php去把这部分逻辑给处理掉了.如果按照你描述的话那么后端应该是给了一个redirect,那你用异步还是可以正常获取到结果的. 不知道这么说能不能懂.
试试curl
curl或者ajax提交
如果是MVC的话,通过View的AJAX提交到Controller,然后通过Curl进行调用相应的接口
http://php.net/manual/zh/book.curl.php
如果用js写,就用ajax;如果是php写,就用curl
他们说的都过于复杂了 如果你只需要跳转页面取值 那就存seesion好了 api存、新页面取
你如果FORM过去的话 就会跳转到别人提供的接口地址了 但是接口不是你的你不能修改 所以一定不能跳转 用AJAX提交或者curl模拟POST提交都可以 这两种方法都会把接口的返回值返回到你自己的页面 方便你接下来处理
他们说的这个跟你说的估计不是同一个事。。。
你说的应该是前端(html)提交数据到一个页面,然后这个页面返回一个json,你想用php保存下来对吧?
那么可以这样写
<code><?php if($_GET['action']=='baocun') { $f=fopen("baocun.txt","a"); fwrite($f,file_get_contents("php://input")) or die("err"); die("ok"); } ?> <script src="jquery.js"></script> <script> function tj() { $.post("你要提交的页面的地址","a="+$("#a")[0].value+"&b="+$("#b")[0].value,function(r){ $.post("?action=baocun",r,function(s){if(s=="ok"){alert("提交成功");}else{alert("提交失败");} }); }); } </script> <form action=""> <input type="text" name="a" id="a"> <input type="text" name="b" id="b"> <input type="button" value="提交" onclick="tj()"> </form> </code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
