【问题标题】:Why does PHP cURL POST request shows as GET?为什么 PHP cURL POST 请求显示为 GET?
【发布时间】:2015-02-02 20:06:18
【问题描述】:

我正在尝试向 api 发送 POST 请求以使用 PHP cURL 创建用户。这是代码示例

<?php
$email="jas@example.com";
$name = "jas";
$data = array(
"user" => array("email"=>$email,"name"=>$name)
);
//encoding to json format
$jsondata= json_encode($data);

$credentials = "username:pass";
$header[] = "Content-Type: application/json";
$header[] =  "Authorization: Basic " . base64_encode($credentials); 
$connection = curl_init();
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($connection, CURLOPT_HTTPHEADER, $header);
curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($connection, CURLOPT_HEADER, false);

//POSTS
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $jsondata);

curl_setopt($connection, CURLOPT_VERBOSE, 1);


curl_setopt($connection, CURLOPT_URL, "http://domain.freshdesk.com/contacts.json");
$response = curl_exec($connection); 
?>

虽然我已经设置了,但看起来它实际上并没有发送帖子

curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $jsondata);

我在 firebug net 选项卡中看到一个 GET 请求。

它真的是一个发布请求吗?因为缩进操作(创建新用户)没有发生,而是列出了所有用户,因为它是一个 GET 请求。

【问题讨论】:

  • 你在客户端看不到 curl 请求,你看到的是对服务器端 curl 调用上升的文件的请求

标签: php http post curl firebug


【解决方案1】:

GET 请求只是您对 PHP 脚本的请求,该脚本然后执行 POST 请求。

您无法在开发者控制台中看到使用 cURL 完成的请求,因为它们是从服务器而不是客户端发送的。

【讨论】:

    【解决方案2】:

    错误在于使用firebug调试这个请求的逻辑。

    您向您的服务器/页面 create-user.php 发送一个 GET 请求。反过来,这个脚本/服务器向 API 站点发送一个 POST 请求。您的 Web 客户端(浏览器)和 firebug 并不“知道”第二部分,它发生在您的服务器上。

    要查看实际的 POST 请求,您应该使用不同的工具。例如,将 POST 请求指向您自己的机器,然后在服务器日志中确认有入站 POST 请求。

    【讨论】:

      猜你喜欢
      • 2017-11-25
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2011-07-04
      • 2021-03-29
      • 2015-08-06
      相关资源
      最近更新 更多