2

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);
}
1
  • George, this is JS, I need to know how it passes through as well, considering it's more than what expected.. thx bud. Commented Nov 8, 2016 at 19:22

1 Answer 1

1

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']));
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! So would my encode look like just 'json_encode($data);'?
and what is baseurl?
Sorry, baseUrl was something I had started to type then changed it to just url.
You're the man. Thank you for this. Sorry if I sounded rude, I get frustrated with people when they answer something but don't explain it exactly. Thanks again!
No offense taken. I have a day job ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.