Hello I found a problem displaying url value.
Is the problem in " Array ( " ?
Array ( [ {
"playabilityStatus": {
"status": "OK", "playableInEmbed": true, "contextParams": "Q0FFU0FnZ0M] => "
}
, "streamingData": {
"expiresInSeconds":"21540", "formats":[ {
"itag": 18, "url": "http://example.com/360.mp4", "mimeType": "video/mp4; codecs=\"avc1.42001E, mp4a.40.2\"", "bitrate": 198298, "width": 638, "height": 360, "lastModified": "1567026236090610", "contentLength": "20775840", "quality": "medium", "qualityLabel": "360p", "projectionType": "RECTANGULAR", "averageBitrate": 198286, "audioQuality": "AUDIO_QUALITY_LOW", "approxDurationMs": "838217", "audioSampleRate": "44100", "audioChannels": 1
}
, {
"itag": 22, "url": "http://example.com/360x1.mp4", "mimeType": "video/mp4; codecs=\"avc1.64001F, mp4a.40.2\"", "bitrate": 404000, "width": 1276, "height": 720, "lastModified": "1567026270240429", "quality": "hd720", "qualityLabel": "720p", "projectionType": "RECTANGULAR", "audioQuality": "AUDIO_QUALITY_MEDIUM", "approxDurationMs": "838217", "audioSampleRate": "44100", "audioChannels": 1
}
, {
"itag": 43, "url": "http://example.com/360x2.mp4", "mimeType": "video/webm; codecs=\"vp8.0, vorbis\"", "bitrate": 2147483647, "width": 640, "height": 360, "lastModified": "1567026831970178", "contentLength": "19705302", "quality": "medium", "qualityLabel": "360p", "projectionType": "RECTANGULAR", "audioQuality": "AUDIO_QUALITY_MEDIUM"
}
], "adaptiveFormats":[
...
$arr is = php file ytb-dl.php
$json_output = json_decode($arr);
foreach ($json_output['streamingData'] as $formats) {
echo $formats['url'];
}
Be aware which structure of data you are using, try code below, is working for me.
Please check if link https://webstickers.000webhostapp.com/ytb-dl.php is active.
$content = file_get_contents('https://webstickers.000webhostapp.com/ytb-dl.php');
$json_output = json_decode($content, true);
foreach ($json_output['streamingData'] as $formats) {
if (!is_array($formats)) {
continue;
}
foreach ($formats as $format) {
echo $format['url'] . PHP_EOL;
}
}
Related
I am using below php code to use json in my file but i am not getting the ouput . I need to print image url from Json array . Can any one help.
Json data is :
{
"pagination": {},
"data": [
{
"id": "1980025670135540608_1039191866",
"user": {},
"images": {
"thumbnail": {
"width": 150,
"height": 150,
"url": "https://scontent.cdninstagram.com/vp/d3d00967ec82131dedfdddb0cb8f8bc7/5CFCE4F1/t51.2885-15/e35/s150x150/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
},
"low_resolution": {
"width": 320,
"height": 320,
"url": "https://scontent.cdninstagram.com/vp/5cb939b696c2d5a0211572bd0e965bee/5CF41C01/t51.2885-15/e35/s320x320/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
},
"standard_resolution": {
"width": 640,
"height": 640,
"url": "https://scontent.cdninstagram.com/vp/411df4ade429230dee0ab3631d25a6eb/5CE56DB3/t51.2885-15/e35/50959433_383283642451967_2367872435860221844_n.jpg?_nc_ht=scontent.cdninstagram.com"
}
},
"created_time": "1550257475",
"caption": null,
"user_has_liked": false,
"likes": {
"count": 0
},
"tags": {},
"filter": "Normal",
"comments": {
"count": 0
},
"type": "image",
"link": "https://www.instagram.com/p/Bt6dts6lWuA/",
"location": null,
"attribution": null,
"users_in_photo": {}
},
{
"id": "1979798389190546391_1039191866",
"user": {
"id": "1039191866",
"full_name": "Jagjeet Kumar",
"profile_picture": "https://scontent.cdninstagram.com/vp/9871a297c832e1b1ef249028856af412/5CFC0D7D/t51.2885-19/11931260_1465802173750145_760962573_a.jpg?_nc_ht=scontent.cdninstagram.com",
"username": "jagjeet_k"
},
"images": {
"thumbnail": {
"width": 150,
"height": 150,
"url": "https://scontent.cdninstagram.com/vp/5f7e27fbf6d878bb85be23b70b637866/5CF8244F/t51.2885-15/e35/s150x150/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
},
"low_resolution": {
"width": 320,
"height": 320,
"url": "https://scontent.cdninstagram.com/vp/16376fe1eb033705d1c7b1f394bef013/5CE38537/t51.2885-15/e35/s320x320/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
},
"standard_resolution": {
"width": 640,
"height": 640,
"url": "https://scontent.cdninstagram.com/vp/c51878930ed8c98a595f16299f5c18d7/5CF9E7CA/t51.2885-15/sh0.08/e35/s640x640/50801938_1432836230186365_4210258679421826307_n.jpg?_nc_ht=scontent.cdninstagram.com"
}
},
"created_time": "1550230381",
"caption": null,
"user_has_liked": false,
"likes": {
"count": 1
},
"tags": {},
"filter": "Crema",
"comments": {
"count": 1
},
"type": "image",
"link": "https://www.instagram.com/p/Bt5qCVCl9PX/",
"location": null,
"attribution": null,
"users_in_photo": {}
}
],
"meta": {
"code": 200
}
}
Php code which I am using :
<?php
$instagram_feed_data = json_decode($mediacount, true);
foreach ($instagram_feed_data->data as $item) {
$img_url = $item['images']['low_resolution']['url'];
?>
<img src="<?= $img_url; ?>">
<?php
}
I have to print the image url which is in low resolution array .
$instagram_feed_data = json_decode($mediacount, true);
Passing a true value to the second parameter of json_decode() tells PHP that you want an array back instead of an object. So you can't dereference it with the object handle like this:
foreach ($instagram_feed_data->data as $item) {
You want an array reference like this:
foreach ($instagram_feed_data['data'] as $item) {
i am trying to get Youtube channel data with that api
https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics&id={ Channel Id }&key={ API }
I used that code to fetch the data
if( $_SERVER[ 'REQUEST_METHOD' ] == 'GET' && !empty( $_GET ) ){
$channel_id = $_GET[ 'channel_id' ];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics&id=". $channel_id ."&key=AIzaSyCKAnI41bI4UGDJgYUcwrhrIfnjoD3uirM",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
?>
<form id="channelIdForm" action="<?php $_SERVER['PHP_SELF']; ?>" method="get">
<input type="text" placeholder="Type channel id" name="channel_id" id="channelId" />
<button type="submit">Get data</button>
</form>
And it appeared like that
{ "kind": "youtube#channelListResponse", "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/pss5QFB6saoevVwzX-8y6f1U75E\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "kind": "youtube#channel", "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/gPFze0Hh7eRu9O5tXiTBVyvCwa8\"", "id": "UC4QYTjhWMxiC6UwvKLVkDDA", "snippet": { "title": "حفيد برايز حفيد برايز", "description": "", "publishedAt": "2018-04-01T17:18:31.000Z", "thumbnails": { "default": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s88-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 88, "height": 88 }, "medium": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s240-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 240, "height": 240 }, "high": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s800-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 800, "height": 800 } }, "localized": { "title": "حفيد برايز حفيد برايز", "description": "" } }, "contentDetails": { "relatedPlaylists": { "uploads": "UU4QYTjhWMxiC6UwvKLVkDDA", "watchHistory": "HL", "watchLater": "WL" } }, "statistics": { "viewCount": "830460", "commentCount": "0", "subscriberCount": "3741", "hiddenSubscriberCount": false, "videoCount": "19" } } ] }
Then i tried to convert that json to an array to pick what i want from it with index
So i tried that instead of $response
$json = $response;
$arr = json_decode($json, true);
$row = array_reduce($arr['items'], function($c, $v){
$c[ key($v) ] = current($v);
return $c;
}, array());
print_r( $row );
I expected to see all the indexes below item but the result was
Array ( [kind] => youtube#channel )
It show just kind i don't know what the problem with so if any one can help i will be appreciated!!
The array_reduce function reduces an array to a single value. But it appears you are interested in multiple key/value pairs. Since json_decode($json, true); already provides you with an associated array you can retrieve specific keys via their names. Example:
$arr = json_decode($response, true);
$newArray = [];
$requiredKeys = ['kind','etag'];
foreach ($requiredKeys as $key){
$newArray[$key] = $arr[$key];
}
It is a very-multi-dimensional array.
Here's some code that prints the contents of an array. If the value it is going to print is an array, it calls itself again. You'll see it puts out Printing $json_arr[items[0][statistics] : followed by a bunch of key->value pairs, so you can find the state you want and you'll know what the parent array(s) are.
<?php
$json='{ "kind": "youtube#channelListResponse", "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/pss5QFB6saoevVwzX-8y6f1U75E\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "kind": "youtube#channel", "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/gPFze0Hh7eRu9O5tXiTBVyvCwa8\"", "id": "UC4QYTjhWMxiC6UwvKLVkDDA", "snippet": { "title": "حفيد برايز حفيد برايز", "description": "", "publishedAt": "2018-04-01T17:18:31.000Z", "thumbnails": { "default": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s88-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 88, "height": 88 }, "medium": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s240-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 240, "height": 240 }, "high": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s800-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 800, "height": 800 } }, "localized": { "title": "حفيد برايز حفيد برايز", "description": "" } }, "contentDetails": { "relatedPlaylists": { "uploads": "UU4QYTjhWMxiC6UwvKLVkDDA", "watchHistory": "HL", "watchLater": "WL" } }, "statistics": { "viewCount": "830460", "commentCount": "0", "subscriberCount": "3741", "hiddenSubscriberCount": false, "videoCount": "19" } } ] } ';
$json_arr=json_decode($json,true);
function print_array($prefix,$arr){
print("printing ".$prefix." :\n");
foreach($arr as $k=>$v){
if(is_array($v)){
print_array($prefix."[".$k."]",$v);
}else{
print("key: ".$k." value:".$v." \n");
}
}
return;
}
print_array("\$json_arr",$json_arr);
?>
I have a JSON from telegram, In php How can find that if it is a text do something, but if its photo, audio or ... do something else. this part is located after date, how can I access it?
I tried by access it by index like ['message'][1] but I got error.
{
"ok": true,
"result": [
{
"update_id": 30213598,
"message": {
"message_id": 41,
"from": {
"id": 102768333,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439233778,
"text": "this is a test"
}
},
{
"update_id": 30213599,
"message": {
"message_id": 46,
"from": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439234126,
"photo": [
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
"file_size": 1923,
"width": 90,
"height": 90
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
"file_size": 24105,
"width": 320,
"height": 320
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
"file_size": 57650,
"width": 640,
"height": 640
}
]
}
},
You need to use json_decode function.
I use foreach loop then echo $item['message']['message_id'] to display message_id in following example.
Try Example
$json = <<<JSON
{
"ok": true,
"result": [
{
"update_id": 30213598,
"message": {
"message_id": 41,
"from": {
"id": 102768333,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439233778,
"text": "this is a test"
}
},
{
"update_id": 30213599,
"message": {
"message_id": 46,
"from": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439234126,
"photo": [
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
"file_size": 1923,
"width": 90,
"height": 90
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
"file_size": 24105,
"width": 320,
"height": 320
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
"file_size": 57650,
"width": 640,
"height": 640
}
]
}
}
]
}
JSON;
$json_data = json_decode($json, true);
foreach($json_data['result'] as $item)
{
if(isset($item['message']['text']))
{
echo $item['message']['text']."<br />";
}
if(isset($item['message']['photo']))
{
foreach($item['message']['photo'] as $photo)
{
echo "file_id: ".$photo['file_id']."<br />";
echo "file_size: ".$photo['file_size']."<br />";
}
}
}
I am getting a response using Unirest library, i need to separate the data, so that based on that data i can call my next query. Here is full json response i am getting while using Unirest library
echo '<pre>'; print_r($response->raw_body); echo '</pre>';
{
"status": "success",
"images": [
"http://www.example.com/12.jpg"
],
"photos": [
{
"url": "http://www.example.com/12.jpg",
"width": 205,
"tags": [
{
"confidence": 0.978945010372561,
"center": {
"y": 64,
"x": 129
},
"height": 79,
"width": 79,
"tid": "31337",
"attributes": [
{
"smile_rating": 0.56,
"smiling": true,
"confidence": 0.56
}
],
"uids": [
{
"confidence": 0.35399999999999998,
"prediction": "SE2",
"uid": "SE2#SEA1"
},
{
"confidence": 0.28999999999999998,
"prediction": "SE1",
"uid": "SE1#SEA1"
},
{
"confidence": 0.16,
"prediction": "Star1",
"uid": "Star1#SEA1"
},
{
"confidence": 0.106,
"prediction": "SE3",
"uid": "SE3#SEA1"
},
{
"confidence": 0.037999999999999999,
"prediction": "SE6",
"uid": "SE6#SEA1"
},
{
"confidence": 0.035000000000000003,
"prediction": "SE5",
"uid": "SE5#SEA1"
},
{
"confidence": 0.017999999999999999,
"prediction": "SE4",
"uid": "SE4#SEA1"
}
]
}
],
"height": 206
}
]
}
What i am trying is to print like this
Confidence : 0.35399999999999998
Similar: Test2
Well, provided it is a valid JSON string, just use a simple json_decode() with true flag so that it returns an array. Example:
$data = json_decode($response->raw_body, true);
$i = 0;
foreach($data['photos'][0]['tags'][0]['uids'] as $value) {
if($i == 5) break;
echo 'Confidence: ' . $value['confidence'] . '<br/>';
echo 'Similar: ' . $value['prediction'] . '<br/><br/>';
$i++;
}
The release of Unirest 2.0 had many improvements including ability to set custom JSON decode flags
this gives you more control over the response body type parsing method (json_decode)
Disclaimer: I'm the author of unirest-php and I work at Mashape.
I am trying to access the urls of the first images that show up for tumblr searches using the tag search api. Below is the json for the test search I'm using.
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": [
{
"blog_name": "m00tzman",
"id": 37918165271,
"post_url": "http://m00tzman.tumblr.com/post/37918165271/test-image-for-tumblr-a…",
"slug": "test-image-for-tumblr-api-thingy",
"type": "photo",
"date": "2012-12-14 18:13:25 GMT",
"timestamp": 1355508805,
"state": "published",
"format": "html",
"reblog_key": "IwlKk9r4",
"tags": [
"g6845e"
],
"highlighted": [ ],
"note_count": 29,
"caption": "<p>test image for tumblr api thingy</p>",
"photos": [
{
"caption": "",
"alt_sizes": [
{
"width": 500,
"height": 628,
"url": "http://25.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
},
{
"width": 400,
"height": 502,
"url": "http://25.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
},
{
"width": 250,
"height": 314,
"url": "http://25.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
},
{
"width": 100,
"height": 126,
"url": "http://24.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
},
{
"width": 75,
"height": 75,
"url": "http://24.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
}
],
"original_size": {
"width": 500,
"height": 628,
"url": "http://25.media.tumblr.com/af60fbe390c487125501e50b37781317/tumblr_…"
}
}
]
}
]
}
My problem is that I am not able to echo out the url for the "original size" image. Here is my code so far:
<?php
$search = file_get_contents('http://api.tumblr.com/v2/tagged?tag=g6845e&api_key=(api key)');
$tumblrdata = json_decode($search, true);
$photos = $tumblrdata['photos']['original_size'];
$url = $photos['url'];
echo $url;
?>
Any guesses on what is going wrong here?
Try
$photos = $tumblrdata['response']['photos'][0]['original_size'];