简体   繁体   English

使用CURL发布JSON,然后转到发布的页面

[英]Posting JSON using CURL then go to the posted page

I have a set of php processing pages that interact together by passing data via get, but now I have to pass JSON between a couple of the processing pages and need the same functionality as when using GET. 我有一组php处理页面,它们通过通过get传递数据相互交互,但是现在我必须在几个处理页面之间传递JSON,并且需要与使用GET时相同的功能。

The current working get method: 当前的工作get方法:

//The guts

header("Location: $moreprocessing_url/?userid=$id");

exit();

Then in the moreprocessing_url picks up: 然后在moreprocessing_url中进行选择:

$userid = $_GET[id];

//More guts

$something = 'important';

header("Location: $public_url/?something=$something");

exit();

So now in the first processing page, instead of sending a simple string, I need to send JSON - so I am using CURL to post the JSON - but after the post I would like the page being posted to to continue the processing and have the original page stop. 因此,现在在第一个处理页面中,而不是发送简单的字符串,我需要发送JSON-所以我正在使用CURL发布JSON-但是发布之后,我希望发布的页面继续进行处理并具有原始页面停止。 The same way the above code works but using CURL/post instead. 以上代码的工作方式相同,但改用CURL / post。 Maybe my understanding of CURL isn't strong enough and this just isn't possible? 也许我对CURL的理解不够强,这是不可能的?

My CURL: 我的CURL:

$curl = curl_init($moreprocessing_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_exec($curl);
curl_close($curl);

exit();

So this returns to the current page where the CURL is happening and not the moreprocessing_url which I want -- is this possible? 因此,这返回到正在发生CURL的当前页面,而不是我想要的moreprocessing_url-这可能吗? Basically I want the page being posted to, to take over and the one sending the CURL to stop. 基本上,我希望页面被发布到该页面,并接管发送CURL的页面停止。

I believe you're trying to do this: 我相信您正在尝试这样做:

  1. Browser asks for guts.php 浏览器要求guts.php
  2. guts.php posts JSON-encoded data to more-guts.php guts.php将 JSON编码的数据发布到more-guts.php
  3. more-guts.php processes the posted JSON and returns a 301 moved temporarily response; more-guts.php处理发布的JSON并返回301移动的临时响应; the new location being public.php?something=important 新位置为public.php?something = important
  4. guts.php receives this response and relays it to the browser guts.php收到此响应并将其中继到浏览器
  5. Browser follows the redirect 浏览器遵循重定向

If this is true then you need little tweaking to your cURL script. 如果是这样,则只需调整一下cURL脚本即可。 Something along these lines: 遵循以下原则:

$curl = curl_init("url.php"); // the URL that processes your POST

curl_setopt($curl, CURLOPT_POST, true); // use POST method
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json")); // set POST header
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // set POST body

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // send output to a variable for processing
curl_setopt($curl, CURLOPT_HEADER, true); // set to true if you want headers in the output
curl_setopt($ch, CURLOPT_NOBODY, true); // set to true if you do not want the body
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // set to false if you want to see what redirect header was sent

$output = curl_exec($curl);
curl_close($curl);

var_dump($output);

Depending values used for CURLOPT_HEADER and CURLOPT_NOBODY , the $output variable will contain the response header, body or both (they're separated by two blank lines). 根据用于CURLOPT_HEADERCURLOPT_NOBODY值, $output变量将包含响应标头,正文或两者(由两行空行分隔)。 If you want something from the header, you can use regular expressions to extract the desired header and send it to the browser. 如果您需要标题中的某些内容,则可以使用正则表达式提取所需的标题并将其发送到浏览器。

OR you can echo the body should you choose to do so. 或者,如果您选择这样做,则可以呼应身体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM