【问题标题】:POST Request in PHP is not returning anythingPHP中的POST请求没有返回任何东西
【发布时间】:2021-10-23 00:42:51
【问题描述】:

我正在尝试使用 Thycotic PAM API。根据他们的文档,以下是一个示例 HTTP POST 请求。显示的占位符需要替换为实际值。

POST /SecretServer/webservices/SSWebservice.asmx/GetUser HTTP/1.1
Host: 192.168.3.242
Content-Type: application/x-www-form-urlencoded
Content-Length: length

token=string&userId=string

我可以从应用程序中获取令牌字符串和用户 ID。有了这些数据,下面是我正在尝试的 PHP 代码

$url = 'https://192.168.3.242/SecretServer/webservices/SSWebservice.asmx/GetUser';
$data = array(
    'token' => 'token_string', 
    'userId' => 8
);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context));
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

我也试过这种方式:

function curl_get_contents($url)
{
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

$url = 'https://192.168.3.242/SecretServer/webservices/SSWebservice.asmx/GetUser?token=token_string&userId=8 HTTP/1.1';

$json = json_decode(curl_get_contents($url));

var_dump($json);

他们都没有返回任何东西。非常感谢任何建议。

【问题讨论】:

  • 您的curl 版本不发送POST 请求,也不发送参数。
  • 谢谢。我是新来的。你能建议更多细节吗?
  • 应该有教程用 curl 发送 POST 数据。

标签: php curl post


【解决方案1】:
curl_setopt($ch ,CURLOPT_POST, 1);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch ,CURLOPT_POSTFIELDS, "token=string&userId=string");

你必须使用这个参数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-11
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 2020-05-17
    • 2019-04-22
    相关资源
    最近更新 更多