我想使用Php在网站上获取Api响应(在nodejs中创建),因此我正在使用 卷曲,但不起作用,我尝试使用以下代码,但对我不起作用(显示空白页),我在哪里错了?这是我的代码
$post = ['email'=> "example@xyz.com",'password'=> "testing"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
print_R($result);
答案 0 :(得分:0)
更改帖子数组
$post = array('email'=> "example@xyz.com",'password'=> "testing");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
print_r($result);
答案 1 :(得分:0)
更改
$result = json_decode($response);
收件人
$result = json_decode($response, true);
然后
echo '<pre>';
print_r($result);
响应:-
Array
(
[statusCode] => 401
[error] => Unauthorized
[message] => Invalid username or password
[responseType] => INVALID_USER_PASS
)
答案 2 :(得分:0)
尝试显示错误,以防错误/警告被抑制。
在php标记后的文件顶部使用这些
ini_set("display_errors", "On");
error_reporting(E_ALL);
还尝试在json_decoding之前打印原始响应,这是因为如果您得到的响应不是有效的json,则在解码后将不会打印出任何内容。
使用此
print_r("The response is: " . $response);
总而言之,您的代码应该看起来像
ini_set("display_errors", "On");
error_reporting(E_ALL);
$post = ['email'=> "example@xyz.com",'password'=> "testing"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://35.154.149.228:8000/api/admin/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
//Printing the original response before trying to decode it
//$result = json_decode($response);
print_r("The response from the server before decoding is: " . $response);
让我们知道您从中得到的确切答复是什么