How to retrive image from parse.com using php - php

I am trying to fetch the image which is already in parse database. In array I am getting the all the fetched data including the image URL but when I am trying to echo that particular URL it is echoing blank!!
Bellow is the code
$object = new ParseQuery("billComputing");
$getdata=$object->descending("updatedAt");
$results = $object->find();
$data=$results[0];
echo "<pre>";
print_r($results);
echo "signature".$sign=$data->get('Signature["url"]');
In print_r() - I am getting this
[Signature] => Parse\ParseFile Object
(
[name:Parse\ParseFile:private] => tfss-8a4dbd13-7e16-4efc-abcf-08e901924f49-sign
[url:Parse\ParseFile:private] => http://files.parsetfss.com/864d47af-9eac-4b75-ae30-17b7352d16ca/tfss-8a4dbd13-7e16-4efc-abcf-08e901924f49-sign
[data:Parse\ParseFile:private] =>
[mimeType:Parse\ParseFile:private] =>
)
I am not able to display the URL. Appreciate help in advance.

Try this :
$object = new ParseQuery("billComputing");
$getdata=$object->descending("updatedAt");
$results = $object->find();
$data=$results[0];
echo "<pre>";
print_r($results);
$file = $data->get('imageColumnName');
$imageURL = $file->getURL();
// you will get image url in $imageURL variable.

Related

How to decode value from json, php

I need to decode the below json from a mobile app
Array
(
[{"unm":"admin","pw”:”password”}] =>
)
and my php code is
$obj1 = print_r($_REQUEST, true); //get $_request variable data(responce of login) data as it is
foreach($obj1 as $key => $value)
{
$obj2 = $key; //get first key
}
$obj3 = json_decode($obj2); //decode json data to obj3
$mob_user_name = $obj2['unm']; //getting json username field value
$mob_user_password = $obj2['pw']; //getting json password field value
Hope this will fix your issue,
Note: content is nothing but which you have received from iOS app
content = Array (
[{"unm":"admin","pw”:”password”}] => )
parse that in php
$json = json_decode($content, true);
print json[0]['unm']; /* prints the username */
print json[0]['pw']; /* prints the password */
{"unm":"admin","pw”:”password”} is an object, and json_decode() will by default build it as so.
$obj = json_decode('{"unm":"admin","pw”:”password”}');
echo $obj->unm;
echo $obj->pw;
If for some reason you want it to be converted to an associative array, set the second parameter of json_decode() to true as specified in the manual.
$arr = json_decode('{"unm":"admin","pw”:”password”}', true);
echo $arr['unm'];
echo $arr['pw'];

How to solve Uninitialized string offset?

I've come to some problems. I've made a wordpress plugin, which automatically gets the most 20 recent Instagram posts and then, in theory, it should make me let to insert the newest image as a shortcode in to the post. Basically, I get an error message displaying:
Notice: Uninitialized string offset: 0 in D:\XEMP\htdocs\xd\wordpress\wp-content\plugins\insta-live\insta-live.php on line 29
Now, the code to reproduce this is:
//define Access token
$accesst= "PUT YOUR INSTAGRAM ACCESS TOKEN HERE";
//userid
$userid= YOUR INSTAGRAM USER ID HERE;
//image count to get
$count=20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
foreach($standardres['data'] as $photo) {
$imageData = base64_encode(file_get_contents($photo['images']['standard_resolution']['url']));
$images[] = '<img src="data:image/jpeg;base64,' . $imageData . '" />';
}
//create functions for shortcodes
function fone($images){
return $images[0]; //naudok tik [one]
}
//shortcodes
add_shortcode( 'one', 'fone');
?>
Any ideas how to solve this? A var_dump() gives me the images above the header..

How to access specific value in php and json

I'm using a token:
$url = "https://api.pipedrive.com/v1/deals?api_token=3bd884bb0078f836a56f1464097fb71eac9d50ce";
and here's the json text(not everything);
{
"success":true,
"data":[{
"id":1,
"creator_user_id":{
"id":1682756,
"name":"Kamilah",
"email":"kamilah#fractal.ae",
"has_pic":false,
"pic_hash":null,
"active_flag":true,
"value":1682756},
"title":"FFS Organization deal"
}]
}
I wanted to display "title" and I always get an error
Notice: Undefined index: creator_user_id in
C:\xampp\htdocs\pipedrive\getjson.php on line 8
Here's my code so far:
$url = "https://api.pipedrive.com/v1/deals?api_token=3bd884bb0078f836a56f1464097fb71eac9d50ce";
$response = file_get_contents($url);
$object = json_decode($response, true);
echo $object['data']['creator_user_id']['title'];
I'm new to json so I'm just practicing and trying to figure out how to echo a specific value in php. Would be appreciated if you can explain exactly how it works when you echo from json to php.
Thank you! :)
First of all you need to debug the array after using json_decode().
<?php
$url = "https://api.pipedrive.com/v1/deals?api_token=3bd884bb0078f836a56f1464097fb71eac9d50ce";
$response = file_get_contents($url);
$object = json_decode($response, true);
echo "<pre>";
print_r($object); // this will print all data into array format.
?>
As per this array, you do not have title index inside the creator_user_id array. title is a separate index.
Also note that, $object['data'] containing two indexes not one. you can get title as:
<?php
foreach ($object['data'] as $key => $value) {
//print_r($value); // this will print the values inside the data array.
echo $value['title']."<br/>"; // this will print all title inside your array.
}
?>
Result:
FFS Organization deal
Google deal

Put API data into a table using php

I am using the insert PHP plugin for wordpress to get API data into my pages.
so I would replace "" on the last line with "[/insert_php]".
my code
[insert_php]
$url = 'https://www.eobot.com/api.aspx?idspeed=285967&json=true';
$args = array (
'method' => 'GET'
);
$response = wp_remote_request($url, $args);
$body = wp_remote_retrieve_body($response);
print_r($body);
[/insert_php]
returns
MiningSHA-256:0.00000000;MiningScrypt:0.00000000;CloudSHA-256:12.72592330;Cloud2SHA-256:0.01894240;CloudScrypt:0.00000000;
I have searched, and maybe I am not using the correct terms, and cannot find my solution or answer. I feel like it should be a lot easier than it is. I feel I should be able to take the array from the body and give each its own variable then use the variables to build a table with PHP. Where am I going wrong? Should I first store this in a php file on my server then create a table, then use the insert php function to build the table that way?
I have tried your code and its working fine for me in local mechine i think you just need to put json_decode for get proper format.
[insert_php]
$url = 'https://www.eobot.com/api.aspx?idspeed=285967&json=true';
$args = array (
'method' => 'GET'
);
$response = wp_remote_request($url, $args);
$body = wp_remote_retrieve_body($response);
print_r($body);
echo "<pre>";
$data = json_decode($body);
print_r($data);
[/insert_php]
Getting this type of output.
stdClass Object
(
[MiningSHA-256] => 0.00000000
[MiningScrypt] => 0.00000000
[CloudSHA-256] => 12.72656020
[Cloud2SHA-256] => 0.01894240
[CloudScrypt] => 0.00000000
)
Please try above code and let me know.
I've checked the code and it is doing what you programmed it do. Please check you're using correct API URL and once you're using the correct API URL use json_decode function to decode json data returned API and add once you've correct data converted into array, you can create table.
For example:
<?php
$body = wp_remote_retrieve_body($response);
$table_data = json_decode($body);
$html = array();
if(!empty($table_data)){
$html[] = '<table>';
foreach($table_data as $rows){
$html[] = '<tr>';
foreach($row as $column){
$html[] = '<td>'. $column . '</td>';
}
$html[] = '</tr>';
}
$html ='</table>';
}
$html = implode("\n", $html);
echo $html;
?>
PS: This code is only an example, please adjust it your data.

How to generate JSON data with PHP?

CREATE TABLE Posts
{
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(200),
url VARCHAR(200)
}
json.php code
<?php
$sql=mysql_query("select * from Posts limit 20");
echo '{"posts": [';
while($row=mysql_fetch_array($sql))
{
$title=$row['title'];
$url=$row['url'];
echo '
{
"title":"'.$title.'",
"url":"'.$url.'"
},';
}
echo ']}';
?>
I have to generate results.json file.
To generate JSON in PHP, you need only one function, json_encode().
When working with database, you need to get all the rows into array first. Here is a sample code for mysqli
$sql="select * from Posts limit 20";
$result = $db->query($sql);
$posts = $result->fetch_all(MYSQLI_ASSOC);
then you can either use this array directly or make it part of another array:
echo json_encode($posts);
// or
$response = json_encode([
'posts' => $posts,
]);
if you need to save it in a file then just use file_put_contents()
file_put_contents('myfile.json', json_encode($posts));
Use this:
$json_data = json_encode($posts);
file_put_contents('myfile.json', $json_data);
You can create the myfile.json before you run the script.But its not compulsory if you have full sudo privileges(read/write permissions(For of you on Mac).
Here is a working Example:
<?php
// data stored in an array called posts
$posts = Array (
"0" => Array (
"id" => "01",
"title" => "Hello",
),
"1" => Array (
"id" => "02",
"title" => "Yoyo",
),
"2" => Array (
"id" => "03",
"title" => "I like Apples",
)
);
// encode array to json
$json = json_encode($posts);
$bytes = file_put_contents("myfile.json", $json); //generate json file
echo "Here is the myfile data $bytes.";
?>
Insert your fetched values into an array instead of echoing.
Use file_put_contents() and insert json_encode($rows) into that file, if $rows is your data.
If you're pulling dynamic records it's better to have 1 php file that creates a json representation and not create a file each time.
my_json.php
$array = array(
'title' => $title,
'url' => $url
);
echo json_encode($array);
Then in your script set the path to the file my_json.php
Here i have mentioned the simple syntex for create json file and print the array value inside the json file in pretty manner.
$array = array('name' => $name,'id' => $id,'url' => $url);
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($array, JSON_PRETTY_PRINT)); // here it will print the array pretty
fclose($fp);
Hope it will works for you....
You can simply use json_encode function of php and save file with file handling functions such as fopen and fwrite.
First, you need to decode it :
$jsonString = file_get_contents('jsonFile.json');
$data = json_decode($jsonString, true);
Then change the data :
$data[0]['activity_name'] = "TENNIS";
// or if you want to change all entries with activity_code "1"
foreach ($data as $key => $entry) {
if ($entry['activity_code'] == '1') {
$data[$key]['activity_name'] = "TENNIS";
}
}
Then re-encode it and save it back in the file:
$newJsonString = json_encode($data);
file_put_contents('jsonFile.json', $newJsonString);
copy
Use PHP's json methods to create the json then write it to a file with fwrite.

Categories