I'm trying to figure out how to encode the following:
$data[] = '';
//check if real player
if($steam_name != null){
$data['valid'] = true;
$data['url'] = "http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key=key&vanityurl=$steam_name";
echo json_encode($data, file_get_contents($data->url));
}else{
$data['valid'] = false;
echo json_encode($data);
}
I understand how to get the data, but it seems to not be sending through.
Thanks!
My attempt as per answer below. This does not work:
$data[] = '';
//check if real player
if($steam_name != null){
$data['valid'] = true;
$url = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?';
$params = [
'key' => 'key',
'vanityurl' => $steam_name,
];
$data['url'] = $url . http_build_query($params);
echo json_encode($data);
}else{
$data['valid'] = false;
echo json_encode($data);
}
You should be using http_build_query() to accomplish that.
$url = 'http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?';
$params = [
'key' => 'abc123',
'vanityurl' => $steam_name,
];
$data['url'] = $url . http_build_query($params);
That will handle the proper encoding for the parameters.
Additionally, $data is an array here, you can't call it like an object in your file_get_contents call. I'm surprised you aren't getting an exception. Also, json_encode doesn't accept parameters like that. Try this:
// Store the API response in your data array
$data['response'] = file_get_contents($data['url']);
// Return it so you can use it
return json_encode($data);
If the response is JSON, you can decode it:
$data['response'] = json_decode(file_get_contents($data['url']));
Related
I am trying to make a code verifier with the following code. I've looked at the answers in StackOverflow but couldn't get the right result.
The code is working fine but I want to echo just two value.
validate.php page code is here:
if(isset($apKey) && isset($code)) {
$en = new En();
$Key = $apKey;
$Codes = $code;
if($Key == null || $Codes == null) {
echo json_encode(['data' => 'No Key or code found, Please try again']);
exit;
}
$response = $en->validate($Key,$Codes);
$result = json_decode($response);
$elements = json_encode(['data' => $result]);
echo $elements;
exit;
}
echo json_encode(['data' => 'No Key or code found, Please try again']);
exit;
Now I am trying to check this validate page results using this code:
$siteurl = urlencode($_SERVER['SERVER_NAME']);
$arrContextOptions = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false
)
);
$file = file_get_contents('http://www.validates.com/validate.php?code=' . $check , false, stream_context_create($arrContextOptions));
$checks = json_decode($file, true);
$elements = $checks['data'];
echo print_r($elements);
So the results something like this:
Array
(
[buyer] => abcd
)
So what I want to do. I want to echo just buyer and error message after this code: $checks = json_decode($file, true);
The error result is here:
{"data":{"error":404,"description":"No sale belonging to the current user found with that code"}}
Like for example:
if(buyer){echo 'true';}
if(error message){echo 'error';}
I guess you can check with array_key_exists if there is error. You can do something like this:
$checks = json_decode($file, true);
$data = $checks['data'];
if (array_key_exists("error", $data))
echo $data["description"];
else
echo $data["buyer"];
I have this API for redtube but when I put this in my .php file and show the site theres comes up this, why the site does not show the videos, its only text on the site when I preview the site. Nothing shows video.
Can someone help me or know what I am doing wrong ?
`
// Simple url builder function.
// You can use CURL for passing HTTP GET parameters and to call the RedTube API.
function RedTubeApiCall($http_server, $params = array())
{
$query_string = '?';
if(is_array($params) && count($params)){
foreach($params as $k=>$v){
$query_string .= $k.'='.$v.'&';
}
$query_string = rtrim($query_string,'&');
}
$content = file_get_contents($http_server.$query_string);
return $content;
}
$http = 'http://api.redtube.com/';
$call = 'redtube.Categories.getCategoriesList';
$params = array(
'output' => 'xml',
'data' => $call
);
$response = RedTubeApiCall($http , $params);
if ($response) {
// handle the response
}
// Simple url builder function.
// You can use CURL for passing HTTP GET parameters and to call RedTube API.
function RedTubeApiCall($http_server, $params = array())
{
$query_string = '?';
if(is_array($params) && count($params)){
foreach($params as $k=>$v){
$query_string .= $k.'='.$v.'&';
}
$query_string = rtrim($query_string,'&');
}
$content = file_get_contents($http_server.$query_string);
return $content;
}
$http = 'http://api.redtube.com/';
$call = 'redtube.Categories.getCategoriesList';
$params = array(
'output' => 'xml',
'data' => $call
);
$response = RedTubeApiCall($http , $params);
if ($response) {
// handle the response
}
?>``
I have a PHP file that grabs a .txt file and decodes the JSON to push the user's e-mail and name entry to my campaign monitor database. The JSON is wonky because new entries are added with a ][ as seen in this example:
[{"email":"drake.scott#test.com","createdate":"2016-03-23 10:44:52","from_email":null,"from_liked_pageid":null,"source":"direct","bonus_entry_url":"\/xxxbyn\/h1lvw5","entry_url":"https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160314-1\/tu%2BcJmnsgyUxE7HM9BCqnRjnza27dGIdr%2BHymHrFmFoprsRgkhZradPrTJ5s18p6ewxYeOOKLpObd%2FWGAS%2BDRw%3D%3D","shareurl":"http:\/\/woobox.com\/qbxbyn\/h1lvw5","picked":"0","actionbutton":"","custom_3_first":"drake","custom_3_last":"scott","custom_4":"4146583264","fullname":"drake scott","id":"vvvbyn"}]
[{"email":"john.roberts#test.com","createdate":"2016-03-23 10:44:52","from_email":null,"from_liked_pageid":null,"source":"direct","bonus_entry_url":"\/xxx\/h1lvw5","entry_url":"https:\/\/admin.woobox.com\/manage\/offers\/entries\/20160314-1\/tu%2BcJmnsgyUxE7HM9BCqnRjnza27dGIdr%2BHymHrFmFoprsRgkhZradPrTJ5s18p6ewxYeOOKLpObd%2FWGAS%2BDRw%3D%3D","shareurl":"http:\/\/woobox.com\/vvvbyn\/h1lvw5","picked":"0","actionbutton":"","custom_3_first":"john","custom_3_last":"adams","custom_4":"4146585018","fullname":"john adams","id":"vvvbyn"}]
The problem I am having right now is uploading the entries to my database. I have managed to strip the ][ and doing a var_dump($content); shows a valid JSON string. However, doing var_dump($user) gives me 0 array(0) { }.
The entries will not upload to my database because there is essentially nothing in my array. Can anyone give me some insight on how to proceed?
<?php
require_once 'csrest_general.php';
require_once 'csrest_subscribers.php';
$auth = array(
'api_key' => 'xxxxxxxxxx');
$wrap = new CS_REST_Subscribers('xxxxxxxxxx', $auth);
$url = 'http://www.mywebsite.com/sweeps/test.txt';
$content = file_get_contents($url);
$content = str_replace('}][{', '},{', $content);
$tmp = explode('][', $content);
foreach ($tmp as $json_part) {
$user = json_decode('['.rtrim(ltrim($json_string, '['), ']').']', true);
$result = $wrap->add(array(
'EmailAddress' => $user->email,
'Name' => $user->fullname,
'Resubscribe' => true
));
}
if($result->was_successful()) {
echo "Subscribed with code ".$result->http_status_code;
}
else {
echo "failed";
var_dump($result->response);
var_dump($user);
}
?>
There are multiple JSON objects concatenated in the file. The easiest way to make the JSON valid is to create an array:
$content = str_replace('][', ',', $content);
$users = json_decode($content, true);
Then you have a multi-dimensional array with multiple users that you will need to decide what you want from it:
print_r($users);
foreach($users as $user) {
echo $user['email'] . "\n";
}
Im getting json files from a site and i want it to find which ever one gets the file and use that one. Im using the $_GET Method too. There are 2 ways of getting the same file but they both require an id or a custom url from steam.
My url example: www.mysite.com?id=123123123
2 ways of getting json file:
http://steamcommunity.com/id/ID/inventory/json/730/2
http://steamcommunity.com/CUSTOMURL/76561198051643107/inventory/json/730/2
How im decoding it:
$id = $_GET['id'];
$url = "http://steamcommunity.com/id/".$id."/inventory/json/730/2";
$content = file_get_contents($url);
$playerinfo = json_decode($content, true);
$InventoryStatus = $playerinfo['success'];
some ref
$id = $_GET['id'];
$handlers =array(
'getByJson','getByDB','getBySomething'
);
$result = array('handler'=>'','data'=>'');
then call $handlers
foreach($handlers as $m){
$methodResult = call_user_func($m,$id);
if($methodResult !==false){
$result = array('handler'=>$m, 'data' => $methodResult) ;
break;
}
}
handler something like this
function getByJson($id){
$url = "http://steamcommunity.com/id/".$id."/inventory/json/730/2";
$string = file_get_contents($url);
$isJson =is_string($string) && is_object(json_decode($string)) && (json_last_error() == JSON_ERROR_NONE) ? true : false;
if($isJson){
$playerinfo = json_decode($content, true);
return $playerinfo['success'];
}
return false;
}
function getByDB($id){
// get $result
if(condition){
return $result
}
return false;
}
function getBySomething($id){
// get $result
if(condition){
return $result
}
return false;
}
I have a json string which is built like so
$response = array();
$response['success'] = true;
$response['paymentRequired'] = true;
$response['url'] = 'http://somefancyweburl.co.uk/members/index.php';
echo urldecode(stripslashes(json_encode($response)));
Works great and is outputting
{"success":true,"paymentRequired":false,"url":"http://somefancyurl.co.uk/members/index.php"}
My app developer has asked for it in this format....
{"data"[{"success":true,"paymentRequired":false,"url":"http://somefancyurl.co.uk/members/index.php"}]}
I cant figure out how to create my array in such a way.
Things I have tried......
calling the array as
$response = array('data')
searching everywhere online. Can't find out how its done. Can someone help me please? Many thanks
$response = array();
$response['success'] = true;
$response['paymentRequired'] = true;
$response['url'] = 'http://somefancyweburl.co.uk/members/index.php';
$array = array('data' => array($response));
echo urldecode(stripslashes(json_encode($array)));
this is version with the data inside [ ]
$response = array();
$response['success'] = true;
$response['paymentRequired'] = true;
$response['url'] = 'http://somefancyweburl.co.uk/members/index.php';
**$rArray = array('data' => array($response));**
echo urldecode(stripslashes(json_encode($rArray)));
Here you need to add additional array key 'data' and another array for response.