我读了一些问答,但仍在为这个问题苦苦挣扎。我需要将特定数组发布到 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);
?>
注意:- 将 UserId
和 Password
值更改为您的真实值。谢谢
关于php - 无法使用 PHP 发出 CURL post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37985025/