Breaking News

php – JSON: Update base64 string using url JSON

Questions:

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.

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,

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

How to&Answers:

// ...
// 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:

 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";
}

?>

Answer:

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.

About rankhawks

Check Also

Learn MongoDB With PHP, PHP Basics & Advanced, Git, JSON,XML

Learn MongoDB With PHP, PHP Basics & Advanced, Git, JSON,XML Master yourself in MongoDB with …

transactions – How can I force that when a php file executes, it executes all the code or nothing?

Questions: I am calling a php file from another php file with include instruction. What …

php – Class ‘Api’ not found in CakePhp ,how to integrate razorpay payment gateway in cakephp

php – image uploading works properly with all devices except iphone

Questions: php (codeigniter): image uploading working properly in all devices but on working on iphone …

string – How to replace Microsoft-encoded quotes in PHP

I need to replace Microsoft Word’s version of single and double quotations marks (“ ” …

Leave a Reply

Your email address will not be published. Required fields are marked *