php - 无法使用 PHP 发出 CURL post 请求

标签 php arrays curl

我读了一些问答,但仍在为这个问题苦苦挣扎。我需要将特定数组发布到 API 并获取另一个数组作为答案。

更新

我用过:

<?php

echo 'Testing cURL<br>';

// Get cURL resource
   $curl = curl_init();
   $data=array(array("UserId"=>"xxxx-10100","Password"=>"pass"));
   $sendpostdata = json_encode( array( "postdata"=> $data ) );
   echo $sendpostdata;
// Set some options - we are passing in a useragent too here
  curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => 'https://cloud.servicebridge.com/api/v1/Login',
  CURLOPT_HTTPHEADER => array('Accept: application/json','Content-Type: application/json'),
  CURLOPT_USERAGENT => 'Codular Sample cURL Request',
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => $sendpostdata
));
// Send the request & save response to $resp
  $resp = curl_exec($curl);
  echo $resp;
// Close request to clear up some resources
  curl_close($curl);

?>

这会导致

测试 cURL {"postdata":[{"UserId":"xxxxx-10100","密码":"pass"}]} { "Data": null, "Success": false, "Error": { "Message": "Invalid UserId: ", "Value": "InvalidUserId", "Code": 9001 } }

没有控制台日志,没有其他消息或线索 我正在尝试实现这个API https://cloud.servicebridge.com/developer/index#/ 到沃德莱斯。

API 需要

{ “用户ID”:“字符串”, “密码”:“字符串” }

你能帮我一下吗?我做错了什么

非常感谢,

扬尼斯

最佳答案

我已经检查了给定的API,它是访问它们的请求参数,我认为您的初始数据(用户名和密码)不是正确进行。请使用此代码:-

<?php
error_reporting(E_ALL); // check all type of errors
ini_set('display_errors',1); // display those errors
// Get cURL resource
$curl = curl_init();
$sendpostdata = json_encode(array("UserId"=>"xxxx-10100","Password"=>"pass"));
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => 'https://cloud.servicebridge.com/api/v1/Login',
  CURLOPT_HTTPHEADER => array('Accept: application/json','Content-Type: application/json'),
  CURLOPT_USERAGENT => 'Codular Sample cURL Request',
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => $sendpostdata
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo $resp;
// Close request to clear up some resources
curl_close($curl);
?>

注意:- 将 UserIdPassword 值更改为您的真实值。谢谢

关于php - 无法使用 PHP 发出 CURL post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37985025/

相关文章:

php - 在 Php $db->select 上选择数据库

php - 上传多个实例时需要重命名文件的帮助

PHP 和 MySQL : Inserting selects wrong $row

python - 如何根据其他 2 列替换 numpy 数组中的值

linux - 如何使用 grep 多重模式并删除第一个模式

c - 如何添加 libcurl 依赖项而不绑定(bind) openssl 版本

git - pod 安装返回 fatal error : SSL certificate issue?

php - 在 laravel 中删除排队的作业

javascript - 这个 Javascript 计数循环是如何工作的? - 创建测验的输出

javascript - 根据数组中坐标的频率创建多个数组