1

I'm new to JSON Code. I want to learn about the update function. Currently, I successfully can update data to the database. Below is the code.

<?php

    require_once "../config/configPDO.php";

    $photo_after = 'kk haha';
    $report_id = 1;

    $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/ot_maintainReport?taskname=&reportStatus=&photoBefore=&photoAfter=". urlencode($photo_after) . "&reportID=$report_id";
    $data = file_get_contents($url);
    $json = json_decode($data);
    $query = $json->otReportList;

    if($query){
        echo "Data Save!";
    }else{
        echo "Error!! Not Saved";
    }

?>

the problem is, if the value of $photo_after is base64 string, which is too large string, it will give the error:

1) PHP Warning: file_get_contents.....

2) PHP Notice: Trying to get property 'otReportList' of non-object in C:

BUT

when I change the code to this,

<?php

    require_once "../config/configPDO.php";

    $photo_after = 'mama kk';
    $report_id = 1;

    $sql = "UPDATE ot_report SET photo_after ='$photo_after', time_photo_after = GETDATE(), ot_end = '20:30:00' WHERE report_id = '$report_id'";
    $query = $conn->prepare($sql);
    $query->execute();

    if($query){
        echo "Data Save!";
    }else{
        echo "Error!! Not Saved";
    }

?>

The data will updated including when the value of $photo_after is in base 64 string.

Can I know what is the problem? Any solution to allow the base64 string update thru json link?

Thanks

2 Answers2

0
// ...
// It's likely that the following line failed
$data = file_get_contents($url);
// ...

If the length of $url is more than 2048 bytes, that could cause file_get_contents($url) to fail. See What is the maximum length of a URL in different browsers?.

Consequent to such failure, you end up with a value of $json which is not an object. Ultimately, the property otReportList would not exist in $json hence the error: ...trying to get property 'otReportList' of non-object in C....

To surmount the URL length limitation, it would be best to embed the value of $photo_after in the request body. As requests made with GET method should not have a body, using POST method would be appropriate.

Below is a conceptual adjustment of your code to send the data with a POST method:

<?php

require_once "../config/configPDO.php";

# You must adapt backend behind this URL to be able to service the
# POST request
$url = "http://172.20.0.45/TGWebService/TGWebService.asmx/ot_maintainReport";

$report_id = 1;
$photo_after = 'very-long-base64-encoding-of-an-image';

$request_content = <<<CONTENT
    {
        "taskname": $taskname,
        "report_id": $report_id,
        "photoBefore": $photoBefore,
        "photo_after": $photo_after,
        "reportStatus": $reportStatus
    }
CONTENT;
$request_content_length = strlen($request_content);

# Depending on your server configuration, you may need to set
# $request_headers as an associative array instead of a string.
$request_headers = <<<HEADERS
Content-type: application/json
Content-Length: $request_content_length
HEADERS;

$request_options = array(
    'http' => array(
        'method' => "POST",
        'header' => $request_headers,
        'content' => $request_content
    )
);
$request_context = stream_context_create($request_options);

$data = file_get_contents($url, false, $request_context);

# The request may fail for whatever reason, you should handle that case.
if (!$data) {
    throw new Exception('Request failed, data is invalid');
}

$json = json_decode($data);
$query = $json->otReportList;

if ($query) {
    echo "Data Save!";
} else {
    echo "Error!! Not Saved";
}

?>
Igwe Kalu
  • 14,286
  • 2
  • 29
  • 39
-1

sending a long GET URL is not a good practice. You need to use POST method with cURL. And your webservice should receive the data using post method.

Here's example sending post using PHP:

//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
            "postvar1=value1&postvar2=value2&postvar3=value3");

// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS, 
//          http_build_query(array('postvar1' => 'value1')));

// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

curl_close ($ch);

// Further processing ...
if ($server_output == "OK") { ... } else { ... }

Sample code from: PHP + curl, HTTP POST sample code?

And all output from the webservice will put in the curl_exec() method and from there you can decode the replied json string.

Mr Hery
  • 829
  • 1
  • 7
  • 25