dongya8378
2017-01-30 11:13
浏览 56
已采纳

如何将'字符添加到表示PHP中WS调用的JSON的字符串中?

I am an abslolute beginner in PHP and moreover in Laravel framework (I came from Java).

I am finding the following problem trying to perform a Guzzle call to a REST web service:

if I do it using mocked creadential data it works fine:

    $response = $client->get('http://localhost:8080/Extranet/login',
        [
            'auth' => [
                'nobili.andrea@gmail.com',
                'pswd'
            ]
        ]);

    $dettagliLogin = json_decode($response->getBody());

    \Log::info('response: '.(json_encode($dettagliLogin)));

But trying to do in this way

    $response = $client->get('http://localhost:8080/Extranet/login',
        [
            'auth' => [
                //'nobili.andrea@gmail.com',
                //'pswd'
                $credentials['email'] . ','
                .$credentials['password']
            ]
        ]);

    $dettagliLogin = json_decode($response->getBody());

    \Log::info('response: '.(json_encode($dettagliLogin)));

it goes into error.

I think that maybe the problem could depend by the fact that the mocked credential contain the ' before and after username and password fields:

'nobili.andrea@gmail.com',
'pswd'

How can I insert it in my dynamic version of code?

图片转代码服务由CSDN问答提供 功能建议

我是 PHP 的绝对初学者,而且 Laravel framework(我来自Java)。

我发现以下问题试图对REST Web服务执行 Guzzle 调用: \ n

如果我使用模拟的creadential数据,它可以正常工作:

  $ response = $ client-> get('http:// localhost:8080  / Extranet / login',
 [
'auth'=> [
'nobili.andrea@gmail.com',
'pswd'
] 
]); 
 
 $ dettagliLogin =  json_decode($ response-> getBody()); 
 
 \ Log :: info('response:'。(json_encode($ dettagliLogin))); 
   
 
  

但是尝试这样做

  $ response = $ client-> get('http:// localhost:8080 / Extranet / login',
  [
'auth'=> [
 //'nobili.andrea@gmail.com',
 //'pswd'
 $ credentials ['email']。','  
。$ credentials ['password'] 
] 
]); 
 
 $ dettagliLogin = json_decode($ response-> getBody()); 
 
 \ Log :: info('response:  '。(json_encode($ dettagliLogin))); 
   
 
 

它出错了。

我想也许是问题 可能取决于被模拟的凭证在用户名和密码字段之前和之后包含'的事实:

 'nobili.andrea@gmail.com'  ,
'pswd'
   
 
 

如何将其插入动态版本的代码中?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyan3237 2017-01-30 11:16
    已采纳

    You need to perform like this:

       $response = $client->get('http://localhost:8080/Extranet/login',
            [
                'auth' => [
                    $credentials['email'],   // <=== don't concatinate the comma here
                    $credentials['password']
                ]
            ]);
    

    No, need to concatinate the array parameters.

    已采纳该答案
    打赏 评论
  • doutan6286 2017-01-30 11:16

    You're putting your credentials into a string, when it expects an array. Try the following;

    $response = $client->get('http://localhost:8080/Extranet/login',
        [
            'auth' => [
                $credentials['email'],
                $credentials['password']
            ]
        ]);
    
    打赏 评论