Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-JSON解码变量的奇怪行为_Php_Json_Curl_Slim - Fatal编程技术网

PHP-JSON解码变量的奇怪行为

PHP-JSON解码变量的奇怪行为,php,json,curl,slim,Php,Json,Curl,Slim,我在我的应用程序中有一个相当奇怪的行为,并且没有想法了。我正在尝试使用Slim创建一个小的restapi。我使用cURL调用页面,请求是JSON格式的。Slim读取主体并使用json_decodevalue进行解码,如果为true,则返回一个数组。请查找post功能: $app->post('/users', function () use ($app, $log) { $body = $app->request()->getBody(); echo $body['

我在我的应用程序中有一个相当奇怪的行为,并且没有想法了。我正在尝试使用Slim创建一个小的restapi。我使用cURL调用页面,请求是JSON格式的。Slim读取主体并使用json_decodevalue进行解码,如果为true,则返回一个数组。请查找post功能:

$app->post('/users', function () use ($app, $log)
{
   $body = $app->request()->getBody();
   echo $body['username']; // Will throw Illegal string offset 'username'

   if (isset($body["username"]) && isset($body["password"]))
   {
      echo $body['username']; // Will work
   }
   else
   {
      throw new \API\Exception\ValidationException("Invalid arguments"); // Is thrown all the time
   }
});
问题1: 即使$body作为数组从json_decode返回,访问成员也会抛出非法字符串偏移错误并停止代码

 var_dump($body); // array(2) { ["username"]=> string(8) "testuser"    ["password"]=> string(8) "testpass" }
 echo is_array($body); // 1
 echo $body['username']; // Illegal string offset 'username'
 echo array_key_exists('username', $body); // Illegal string offset 'username'
我找到的解决方案是放置isset$body[key],代码将继续运行,就像什么都没有发生一样。假设这是一个补丁,但我不喜欢。我想了解为什么会发生这种情况。然而,这仍然让我想到了问题2

问题2: 如果我试图将异常放置在isset块之外,它每次都会抛出异常,即使参数是否有值

这对我来说毫无意义,我尝试了我想到的一切。我还有一个GET/users/:username,在这里我还使用一个异常来验证用户名,一切正常。它仅在从JSON编码的POST读取参数后发生

我已经在iisserver下测试了这两种服务器,它们都在Windows和Apache上,php5.5和5.6都在x64上

我做错了什么?先谢谢你

编辑:

还有cURL调用:

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_1);
curl_setopt($ch, CURLOPT_CAINFO, "pemPath"); //Intentionally deleted the CAinfo path
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, "myauth"); //Intentionally deleted credentials
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, "https://localhost/api/v1/users");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-HTTP-Method-Override: POST", "Content-Type: application/json; charset=UTF-8"));
$postArr = array("username" => "testuser", "password" => "testpass");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
$result = curl_exec($ch);

&应该是&?您可以为isset提供多个参数,它将测试所有参数。_array$body是否真的返回数组而不是true?我怀疑您在某处输入错误,因此在尝试访问username元素时使用了不同的变量。感谢您的回复。抱歉,我在这里输入了一个错误,代码中没有&&。不幸的是,这不是因为这个。正如我所说,这很奇怪。如果我用一个简单的回显替换异常,那么无论是否存在,代码都可以正常工作。