【问题标题】:What Does the Page I'm Sending a cURL Post to Have to Do to "Respond"?我要发送 cURL 帖子的页面必须做什么才能“响应”?
【发布时间】:2013-03-15 10:10:32
【问题描述】:

我在网站上有一个非常简单的脚本。该脚本正在向我可以控制的另一个站点发送 HTTP POST。我了解如何使用 cURL 来发送 POST,但是我发送它的脚本必须在它上面做出响应吗?

例如,这里是发送 POST 的代码(如果此脚本回显来自站点 B 的响应,我希望它,但我什至不知道如何让站点 B 响应该帖子):

<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.mysite.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

echo $server_output;

?>

然后是站点 b,在上面的 POST 被发送到的 URL 处,有以下代码:

<?php

$postvar1 = $_POST['postvar1'];

if ($postvar1="apples and oranges") {
$echo "This is what you see if postvar1 has been posted and is equal to apples and oranges"; } else {echo "this is what you see if postvar1 has not been posted or is not equal to apples and oranges"; }


?>

站点 B 的脚本需要做什么才能根据脚本中的“if 逻辑”以屏幕上显示的文本进行响应?

【问题讨论】:

  • 你为什么要$echo

标签: php post curl


【解决方案1】:

站点 B 的响应是发送的任何输出。您的代码中有错误:

$echo "This is what you see..."

应该是

echo "This is what you see..."

我想这就是你没有看到任何回复的原因。

为了查看请求是否成功,您可以(在测试期间)在脚本的开头或结尾将一些字符串作为站点 B 回显。示例:

<?php
echo 'Hi, I\'m site B!<br/>';
$postvar1 = $_POST['postvar1'];

【讨论】:

    【解决方案2】:

    您只需执行普通的 HTML 输出(或 JSON,或其他),就像您响应 Web 浏览器完成的普通 POST 一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多