【发布时间】:2017-09-05 09:18:32
【问题描述】:
我有以下数组:
Array
(
[0] => 32
[1] => Array
(
[uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131
[conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3
[status] => failed
[direction] => outbound
)
)
并希望获得 id 和状态,我有以下代码:
<?php
require __DIR__ . '/../env.php';
//TransferLog Tropo
ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
header('Content-Type: application/json');
$body = file_get_contents('callnexmo.log');
//$body = json_decode($json, true);
//$id=$_GET['id'];
//$body=array($id, $json);
//$req_dump = print_r($body, true );
//$fp = file_put_contents('callnexmo.log', $req_dump);
$conn = new mysqli($servername, $username, $password, $database);
//foreach ($body as $value)
//{
$status=$body[1]['status'];
$to=$body[1]['to'];
$from=$body[1]['from'];
$id=$body[0];
echo " body = $body";
var_dump($body);
echo " status = $status";
var_dump($status);
echo " id = $id";
var_dump($id);
//rest of the code
但是输出让我明白了
Warning: Illegal string offset 'status' in /opt/lampp/htdocs/buttoncall/skeleton-application/ficheiros/records/teste.php on line 23
Warning: Illegal string offset 'to' in /opt/lampp/htdocs/buttoncall/skeleton-application/ficheiros/records/teste.php on line 24
Warning: Illegal string offset 'from' in /opt/lampp/htdocs/buttoncall/skeleton-application/ficheiros/records/teste.php on line 25
body = Array
(
[0] => 32
[1] => Array
(
[uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131
[conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3
[status] => failed
[direction] => outbound
)
)
string(264) "Array
(
[0] => 32
[1] => Array
(
[uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131
[conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3
[status] => failed
[direction] => outbound
)
)
"
status = rstring(1) "r"
id = Astring(1) "A"
我一直在做与这个类似的逻辑,并且它有效,但现在我似乎可以将手指放在错误中.. 有人可以帮我吗?
【问题讨论】:
-
您误解了传入的数据。你有一个“看起来”像一个完全有效的数组转储的文本字符串。您需要以不同的方式存储数据,以便在需要时可以轻松访问。如果此文件超出您的控制范围,您将不得不使用正则表达式或其他方式破解它。
-
你可以这样砍它:regex101.com/r/3CXktP/1
-
您要我发布答案吗?
-
文件将通过 webhook,我希望稍后将它们保存在数据库中..