用于访问和显示来自web服务的信息的GET方法


GET method PHP for accessing and displaying Information from web services

我想知道如何在http头字段传递变量?我尝试了这个代码,它返回给我status 500 error

echo $token;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'X-Auth-Token: $token'));                                                                     

这是我收到的错误:

"时间戳"

{: 1436349947368,"状态":500年,"错误":"内部服务器错误"、"异常":"java.lang.RuntimeException"、"消息":"org.springframework.security.core.userdetails.UsernameNotFoundException:" User name not found","path":"/api/customers/new"}

我已经将从web服务收到的令牌存储在$token中,您能告诉我哪里出错了吗?

期待您的回复,

谢谢

问题似乎是您使用了一个单引号,但希望传递令牌var。

应:

echo $token;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
"X-Auth-Token: $token"));         

我试了一下:

<?php
session_start();
?>
$_SESSION["Token"]=$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
"Content-Type: application/json",                                                                                
"X-Auth-Token: " . $_SESSION["Token"])); 

现在运行正常......

谢谢