How to re-build URL to make sure parameter sent - php

I'm making a function that can perform queries to maps.googleapis.com. I use the library GuzzleHttp assistance.
The problem I faced is when I create a function and try to access the functionality by adding parameters in URL. Looks like this function doesn't known parameters (unallocated) consequently the function that I made did not work properly.
My Controller
public function actionGetalldata() {
$url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key=alskdsbeKASDJldkd&types=geocode&language=id&input=paci";
$client = new GuzzleHttp\Client();
$res = $client->request('GET', $url);
$data = $res->getBody();
$array = json_decode($data, true);
foreach ($array['predictions'] as $key) {
$detail = $this->getLatLong($key['place_id']);
}
}
You see... When I try to loop my whole data, I call getLatLong function but always fail because the function can not accept the parameters that I provide.
Below is the function
function getLatLong($placeid) {
$url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=".$placeid."&key=alskdsbeKASDJldkd";
$client = new GuzzleHttp\Client();
$res = $client->request('GET', $url);
$data = $res->getBody();
$array = json_decode($data, true);
$resultx = $array['result'];
$id = $resultx['id'];
$placeid = $resultx['place_id'];
$alamat = $resultx['adr_address'];
$lat = $resultx['geometry']['location']['lat'];
$long = $resultx['geometry']['location']['lng'];
$allData = [
'id' => $id,
'place_id' => $placeid,
'address' => $alamat,
'lat' => $lat,
'long' => $long
];
return $allData;
}
It looks like the $placeid variable is the cause.
If I directly place a static placeid value, this function working.
How can I fix it? Please help
Thank you

Related

PHP - Combine multiple API endpoints in array

I am trying to create a small function to load data from different API endpoints into one array to work with / load data from. Right now my code is looking as follow;
$url1 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/uhmotNBAQ6-0vi9PFysRBg/members';
$url2 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/DWkD4B0uSsGQoryrz8Nuwg/members';
$url3 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/wSbCgimqTPy470n-wDQXPQ/members';
$url4 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/nmc0HQW-TZirTlnGzwbF-w/members';
$url5 = 'https://gameinfo.albiononline.com/api/gameinfo/guilds/WpV4yaVxSLW8QXH2Be40cA/members';
$api_endpoint1 = json_decode(file_get_contents($url1), true);
$api_endpoint2 = json_decode(file_get_contents($url2), true);
$api_endpoint3 = json_decode(file_get_contents($url3), true);
$api_endpoint4 = json_decode(file_get_contents($url4), true);
$api_endpoint5 = json_decode(file_get_contents($url5), true);
$merged_data = array();
foreach ($api_endpoint1 as $data) {
foreach ($api_endpoint2 as $data2) {
$merged_data[] = array_merge($data, $data2[$data['Name']]);
}
}
var_dump($merged_data);
It doesn't work as I tried so many things the past two days, I came to search for help.
Huge thanks to #barrycarter for his help in pushing me into the right direction.
The actual code needed to be like this:
$url1 = 'gameinfo.albiononline.com/api/…';
$url2 = 'gameinfo.albiononline.com/api/…';
$url3 = 'gameinfo.albiononline.com/api/…';
$url4 = 'gameinfo.albiononline.com/api/…';
$url5 = 'gameinfo.albiononline.com/api/…';
$merged_urls = [$url1, $url2, $url3, $url4, $url5];
$api_endpoint = [];
foreach ($merged_urls as $urls) {
$api_endpoint[] = file_get_contents($urls);
}
// Gives all DATA from all Guilds members
// var_dump($api_endpoint);
$merged_arrays = array_merge(json_decode($api_endpoint[0], true), json_decode($api_endpoint[1], true), json_decode($api_endpoint[2], true), json_decode($api_endpoint[3], true), json_decode($api_endpoint[4], true));
foreach ($merged_arrays as $query) {
echo $query['Name'];
echo '<br>';
}

JSON encode an array with file_get_contents

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']));

Only text on the website when i put the api

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
}
?>``

read xml on url by curl in magento 2

as title, i want to read to parse xml on url by using curl in magento 2. but it not worked.
here is my code.
$om = \Magento\Framework\App\ObjectManager::getInstance();
$curl = $om->get('Magento\Framework\HTTP\Adapter\Curl');
$curl->setConfig([
'timeout' => 5
];
$curl->write(\Zend_Http_Client::GET,$link);
$data = $curl->read();
if ($data === false) {
return false;
}
$data = preg_split('/^\r?$/m', $data, 2);
$data = trim($data[1]);
$curl->close();

What is wrong with my PHP and json

I am trying to get a response from my json array:
stdClass Object ( [Foo] => stdClass Object ( [id] => 0001 [name] => Foo [profileIconId] => 550 [summonerLevel] => 30 [revisionDate] => 1408463933000 ) )
using my current code, I know that it is really easy to solve - but I don't know what I am doing wrong as I can't find anything similar to this from what I am searching:
api.php:
<?php
class runeapi {
const get_id_na = 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/';
const get_id_euw = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/';
const key = '...';
public function getID($summoner_name) {
$name = $summoner_name;
$call = self::get_id_euw .$name;
return $this->request($call);
}
private function request($call) {
$url = $call. '?api_key=' .self::key;
$json = file_get_contents($url);
$decode = json_decode($json);
$result = $decode; //<-- This is the Issue.
return $result;
}
}
?>
testing.php:
<?php
include('api.php');
$summoner_name = 'Foo';
$test = new runeapi;
$r = $test->getID($summoner_name);
print_r($r);
?>
$r returns $result
I'd like to be able to call for id but no matter where I tried looking, I couldn't find an example similar to what I have.
What I've tried:
$decode->{'id'};
$decode{'id'};
I believe this will work for you
private function request($call) {
$url = $call. '?api_key=' .self::key;
$json = file_get_contents($url);
return $json;
}
No need to decode it.
I had to add another variable to request():
public function getID($summoner_name) {
$name = $summoner_name;
$call = self::get_id_euw .$name;
return $this->request($call, $name); //<-- added $name
}
private function request($call, $name) { //<-- added $name
$url = $call. '?api_key=' .self::key;
$json = file_get_contents($url);
$decode = json_decode($json);
$result = $decode->{$name}->{'id'}; //<-- added $name
return $result;
}

Categories