以数组形式传递数据-PHP
我将数据和文件作为数组传递给API 但在API级别,我会检查收件人是否为数组格式 在我的代码中,我将数据作为数组传递,但API返回一个错误,即我的数据不是数组形式 请问我在这里做错了什么 错误 确保以数组形式返回目标字段 客户端以数组形式传递数据-PHP,php,laravel,Php,Laravel,我将数据和文件作为数组传递给API 但在API级别,我会检查收件人是否为数组格式 在我的代码中,我将数据作为数组传递,但API返回一个错误,即我的数据不是数组形式 请问我在这里做错了什么 错误 确保以数组形式返回目标字段 客户端 if(function_exists('curl_file_create')) { $cFile = curl_file_create($file_name_with_full_path); } else { $cFile = '@' . realpat
if(function_exists('curl_file_create'))
{
$cFile = curl_file_create($file_name_with_full_path);
} else {
$cFile = '@' . realpath($file_name_with_full_path);
}
$post = array( 'destination' => ['00448923202'], 'file'=>$cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$result=curl_exec ($ch);
return $result;
curl_close ($ch);
API级别
$destination = $request->post('destination');
if (!is_array($destination)) {
return response()->json([
'status' => 'error',
'message' => 'Make sure you are passing the destination field as an array'
]);
}
我不知道您从哪里获得
收件人
,因为它不在您显示的请求中。看起来你混合了一些变量
其次,您缺少了json\u decode()
,因为您发送的是json,需要在接收后对其进行解码
关于您要查找的函数array\u key\u exists()
或干脆isset()
例如:
$test = ['destination' => '12345'];
var_dump(isset($test['destination']));
var_dump(array_key_exists('destination', $test));
如果您使用的是Laravel5.x,那么您将使用
$request->get('recipient')
来获取参数,无论是通过POST
还是get
方法将永远不会执行,因为它发生在返回之后
——这只是需要注意的一点。发送数据时对其进行编码,接收数据时必须对其进行解码。Try:json_decode()我猜你需要在API级别解码你的json??为什么你要将目的地
发送到你的后端,但要检查收件人
?@NicoHaase,我混淆了我的变量,但进行了更新。所以isset或array_key_exists()解码json你的意思是?@CodeNewbie no,json_decode()
解码json(?)<代码>isset()检查isset和数组。。。哥们,函数名说明了它们的功能?!请访问php.net并阅读相关内容。。您甚至没有尝试代码示例,…您的回答基本上对我的问题没有任何意义。您能进一步解释一下为什么您认为这可以解决问题吗?是的,$request->post('recipient')
不起作用。如果要检查Client-Side`是否正确发送参数,可以执行dd($request->all())
,如果返回空数据,则问题出在客户端
。