I am currently working on Roblox API. I am stuck on one question. Reason :
I have this link https://avatar.roblox.com/v1/users/2/currently-wearing.
This shows what specified users have equipped on them. this link right here shows this:
{"assetIds":[382537569,607702162,607785314]}
My goal is to get the assetIds to string.
I tried this:
<?php
$id = 2;
$ch = file_get_contents("https://avatar.roblox.com/v1/users/$id/currently-wearing");
$ch = curl_init($ch);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$data = json_decode($data);
$id = "$data->assetIds";
echo $id;
But it Shows Array. I need some help.
The cURL is not necessary as the file_get_contents() get you the data you appear to require
$id = 2;
$ch = file_get_contents("https://avatar.roblox.com/v1/users/2/currently-wearing");
$data = json_decode($ch);
foreach ($data->assetIds as $id){
echo $id . PHP_EOL;
}
RESULT
382537569
607702162
607785314
Related
I am trying to display the multiple image on each account on a different API URL!
On each account displaying the image like
Bob has 4 images on his account and it will display that all on data!
I can only fetch 1 image on when adding integer!
Example:
$image = $data["result"][THIS IS INTEGER]["image"];
$image = $data["result"][THIS IS INTEGER]["image"];
$image = $data["result"][THIS IS INTEGER]["image"];
$image = $data["result"][THIS IS INTEGER]["image"];
How can I fetch each images using array on curl
The image below is the result I want
This is the sample code I made
<?php
$query =
"SELECT
*
FROM
user
";
$result = mysqli_query($conn, $query);
$total=0;
while($row = mysqli_fetch_array($result))
{
$id= $row['id'];
$age = $row['age'];
//* first API//
$url = 'https://api.sample/.$id;
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);
$data = json_decode($r, true);
$email = $data[email];
This is the image part
//* second API //
$url = 'https://api.second-sample/.$id;
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r=curl_exec($ch);
curl_close($ch);
$data = json_decode($r, true);
$image = $data["result"][ THIS INTEGER ]["image"];
?>
<?php ECHO the DATA HERE ?>
<?php } ?>
Second API sample DATA result
{"total:4","results":
[
"id":"0"{
"id":"32",
"name-of-image":"lovelife",
"image":"http://sample-storage-image.com/image1.png"
}
"id":"1"{
"id":"52",
"name-of-image":"forrest",
"image":"http://sample-storage-image.com/image2.png"
}
"id":"3"{
"id":"2",
"name-of-image":"sand",
"image":"http://sample-storage-image.com/image3.png"
}
"id":"3"{
"id":"5",
"name-of-image":"water",
"image":"http://sample-storage-image.com/image4.png"
}
]
}
Assuming you iterate correctly through each Image you are making only one major mistake!
If you save it to $image = ... you overwrite the $image every time you get the next image...
you should:
$image[] = $data["result"][ THIS INTEGER ]["image"];
or
$image[ THIS INTEGER ] = $data["result"][ THIS INTEGER ]["image"];
and then display each: for example:
print_r($image[0]);
or
print_r($image[1]);
or
print_r($image[ THIS INTEGER ]);
(I'm assuming that THIS INTEGER is unique!)
etc' at each correct line..
It is one user... however there are multiple images - this requires and array....:)
As mentioned earlier , i am working on the local server as of now in xampp. I have created 2 files index.php and test.php. What i want to achieve is that , index.php will send json data to test.php , with the json data received , the test.php is able to use that json data to turn the statistics into graph.
I am working with the first step , but however , nothing seems to display on my test.php when i tried to do a var_dump($data) , and what i get is NULL. Tried alot of solutions online but none to seems to fix it. I am relatively new to this , so really thanks and appreciate of your help. First php is index.php , second php is test.php.
Do i require a live server as of now in order to see the results in test.php or local server unable to display the result?
<?php
$array = array();
$product = array();
$product[0]['id_product'] = 'A01';
$product[0]['name_product'] = 'Sandal';
$product[0]['price_product'] = '500';
$product[1]['id_product'] = 'A02';
$product[1]['name_product'] = 'Shoes';
$product[1]['price_product'] = '2500';
$array['id'] = '123';
$array['note'] = 'this is my short example';
$array['data'] = $product;
$data = json_encode($array);
$ch = curl_init('http://localhost:8080/practice3/test.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($data));
$result = curl_exec($ch);
var_dump($result);
?>
```````````````````````````````````
<?php
$fp = fopen('php://input', 'r');
$raw = stream_get_contents($fp);
$data = json_decode($raw,true);
echo "hello";
echo $data['id'];
echo $data['note'];
foreach ($data['data'] as $key) {
echo 'id_product : '.$key['id_product'].'<br/>';
echo 'name_product : '.$key['name_product'].'<br/>';
echo 'price_product : '.$key['price_product'].'<br/>';
}
?>
````````````````````````````````````
$text="text.";
//fetch from DB
$sql=mysql_query("SELECT * FROM `employee`");
while($query=mysql_fetch_array($sql))
{
echo $employee_mobile=$query['employee_mobile'];
echo $url="http://mobile.ssexpertcompu.com/vendorsms/pushsms.aspx?user=MYUSERNAME&password=MYPASSWORD&msisdn=".$employee_salary."&sid=78NSL&msg=".$text."&fl=0&gwid=2";
$url = str_replace(" ","%20",$url); // to properly format the url
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
echo $data = curl_exec($ch);
curl_close($ch);
}
First employee record are fetched from DB,then using curl I'm sending message to their respective mobile numbers.The code for sending messages works fine without loop. I've seen some post how to use curl in a loop, but couldn't modify to my needs.
Looks like you have not defined $employee_salary and should exchange it with $employee_mobile:
echo $employee_mobile = $query['employee_mobile'];
echo $url = "http://mobile.ssexpertcompu.com/vendorsms/pushsms.aspx?user=MYUSERNAME&password=MYPASSWORD&msisdn=" . $employee_mobile . "&sid=78NSL&msg=" . $text . "&fl=0&gwid=2";
I'm trying to get the redirect url from a stream using php.
Here's the code I have right now:
<?php
$stream = 'https://api.soundcloud.com/tracks/178525956/stream?client_id=XXXXXX';
$ch = curl_init($stream);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = curl_exec($ch);
echo $url;
curl_close($ch);
?>
Which outputs as a string:
{"status":"302 - Found","location":"THE_URL_I_WANT"}
So how ould I go about getting the url I want as a variable?
Thanks
It's simple use json_decode
$data = json_decode($url);
$your_url = $data->location;
How about
$data = json_decode($url);
$location = $data["location"];
I have a twitter api php script successfully spits out the last 100 of my followers
$flwrs_url = "http://api.twitter.com/1/statuses/followers/exampleuser.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $flwrs_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
foreach($response as $friends){
$id = $friends['id'];
$screen_name = $friends['screen_name'];
....
(I used exampleuser instead of my own account)
How do I extend this to include more or all of my followers?
Thank you
According to the Twitter API Documentation for GET followers/ids the request should return up to 5000 followers.
However, if not all followers are returned by the API, there should be a next_cursor element in the response which can be used to retrieve the next batch of followers. You only have to append the value of next_cursor as cursor to the request (see also the API documentation).
Please note that you are using Version 1 of the Twitter API which has been deprecated recently. That is, it will stop working probably early next year. You should upgrade to Version 1.1 as soon as possible. There are new guidelines in place, one of them is that all requests must be authenticated with oAuth.
Thanks for the answer Florian. BTW stumbing around I think I found the correct way to do what I was looking for. Correct me if I'm wrong.
after using the:
$code=$tmhOAuth->request('GET', $tmhOAuth->url('1/followers/ids'),
array('screen_name' => $user, 'cursor' => $cursor));
technique to grab all 5000 followers (user ids). I use the following code to grab batches of 100 (user details) at a time:
$status_url = "http://api.twitter.com/1/users/lookup.json?user_id=";
$lastNum=$last; // $lastNum=100;
$i=$first; // $i=0;
while($i<$lastNum){
if ($i==($lastNum-1)){
$status_url = $status_url . "$followers[$i]";
}else{
$status_url = $status_url . "$followers[$i],";
}
$i++;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $status_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
$i = 0;
foreach($response as $friends){
$id = $friends['id'];
$screen_name = $friends['screen_name'];
$name = $friends['name'];
$thumb = $friends['profile_image_url'];
$url = $friends['screen_name'];
$location = $friends['location'];
$description = $friends['description'];
echo "$i) $id $screen_name $name $location $description<br />";
$i++;
}
I realize I need to put "sleep" in between each of these "batches of 100", but I'm not quite sure how much to use.