-1

I have the following Array:

Array
(
    [0] => 32
    [1] => Array
        (
            [uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131
            [conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3
            [status] => failed
            [direction] => outbound
        )

)

And wish to get the id and the status, I have the following code:

<?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

But the output gets me

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"

I have been doing logic similar to this one, and it has work, but now I can seem to put my finger in the errror.. Can some one help me?

ines pelaez
  • 473
  • 1
  • 3
  • 10
  • 1
    You are misunderstanding the incoming data. You have a string of text that "looks" like a perfectly valid array dump. You need to store your data differently so that it is readily accessible when you want it. If this file is out of your control, you will have to hack at it with regex or something. – mickmackusa Sep 05 '17 at 09:23
  • You might chop at it like this: https://regex101.com/r/3CXktP/1 – mickmackusa Sep 05 '17 at 09:30
  • Would you like me to post an answer? – mickmackusa Sep 05 '17 at 09:33
  • Possible duplicate of [PHP Warning: Illegal string offset](https://stackoverflow.com/questions/16088530/php-warning-illegal-string-offset) – mickmackusa Sep 05 '17 at 09:36
  • The files will come through an webhook, and i wish to later save them in a database.. – ines pelaez Sep 06 '17 at 14:47

3 Answers3

0

$body is string not array, because file_get_contents returns the file content which is a string. So you can't work with $body like with array.

You can parse data from a string with regexp (take a look into pre_match and preg_match_all functions).

Neodan
  • 5,154
  • 2
  • 27
  • 38
0

In reality, your first assertion is incorrect. You have a string that "looks" like an array.

var_dump() tell you this at the start of its output: string(264).

Based on your header() declaration, it appears that you want to process a json string.

If you have control over the content of callnexmo.log, I recommend that you store your php array data as a json string. So that it looks like this:

[32,{"uuid":"92bbf05c-b49e-4950-9d4a-69c226325131","conversation_uuid":"CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3","status":"failed","direction":"outbound"}]

Then when you want to handle the data in the .log file, you can just json_decode($string,true) the string to generate a readily accessible array.

If you don't have control over the content of callnexmo.log AND the structure of the array is reliable, then the best you can do it try to parse it with regex, but this may or may not be a reliable endeavor.

Given your posted data, here's a simple demonstration: (Pattern Demo) (PHP Demo)

$body='Array
(
    [0] => 32
    [1] => Array
        (
            [uuid] => 92bbf05c-b49e-4950-9d4a-69c226325131
            [conversation_uuid] => CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3
            [status] => failed
            [direction] => outbound
        )

)
';
preg_match_all('/\[([^]]+)\] => (?:Array(*SKIP)(*FAIL)|(.*?)\s+$)/m',$body,$out);
var_export(array_combine($out[1],$out[2]));

Output:

array (
  0 => '32',
  'uuid' => '92bbf05c-b49e-4950-9d4a-69c226325131',
  'conversation_uuid' => 'CON-ee2287cb-ddb7-47e3-9595-0b4d36ac57e3',
  'status' => 'failed',
  'direction' => 'outbound',
)

It is not a perfect replica of your expected array -- it is flattened -- but it should allow you to fetch the values that you need.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

I created an array, and got the values from there.

<?php

require __DIR__ . '/../env.php';
//TransferLog Nexmo

header('Content-Type: application/json');
$json = file_get_contents('php://input');
$request = json_decode($json, true);

$id=$_GET['id'];
$body=array($id, $request);

$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];
ines pelaez
  • 473
  • 1
  • 3
  • 10