Fetch something from steam api - php

I'm working on a PHP script and I want it to fetch this from steam api:
Steam api example
This is my code so far:
<!DOCTYPE html>
<html>
<head>
<title>
Test
</title>
</head>
<body style="text-align:center;">
<form method="post">
<input type="submit" value="OK" name="kk"/>
</form>
<?php
if(isset($_POST['kk'])) {
$json = file_get_contents('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=STEAM API KEY HERE');
$obj = json_decode($json);
echo $obj->result;
}
?>
</head>
</html>
How can I fetch it?

You are trying to echo an stdClass as per your comments. That is why the error.
You can access the data (marked in red in the attached image ) like this
<?php
if(isset($_POST['kk'])) {
$json = file_get_contents('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=STEAM API KEY HERE');
$obj = json_decode($json);
echo $obj->result->services->SessionsLogon;
echo $obj->result->services->SteamCommunity;
echo $obj->result->services->IEconItems;
}
If you are more comfortable with arrays instead of stdClass you can use $obj = json_decode($json, true) and access the elements with echo $obj['result']['services']['SessionsLogon'] etc.
To get a human readable output of the stdClass or the array you should use pre and print_r instead of just var_dump. For eg. echo '<pre>' . print_r($obj, true) . '</pre>';

Related

XML Tags Being Removed from Data in PHP Page Retrieve from AWS DynamoDB

Sketched out a PHP page to pull data from a DynamoDB table. The page pulls a field with XML data, submissionMessageSent, and displays it twice.
First Occurrence: Looks like the data with the XML tags missing.
Second Occurrence: Looks like the data with the XML tags in place.
Between the two occurrences the variable isn't updated by code in the page. The data is stored w/tags in Dynamo as a big old string.
Why are the XML tags stripped out in one case but not the other?
<html>
<head>
<title>Spyglass</title>
<link rel="stylesheet" type="text/css" href="./style.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="get">
Submission Identifier: <input name="submission_id" type="submission_identifier" size="45" value="<?php $submissionId ?>"/>
<input type="submit">
</form>
<?php
date_default_timezone_set('UTC');
$configs = include('./config.php');
require './vendor/autoload.php';
use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\DynamoDb\Marshaler;
if ( isset($_GET["submission_id"]) && $_GET["submission_id"] != "" ) {
$aws_credentials = new Aws\Credentials\Credentials(
$configs['aws_account_key'],
$configs['aws_account_secret']
);
$sdk = new Aws\Sdk([
'region' => $configs['aws_region_identifier'],
'version' => "2012-08-10",
'credentials' => $aws_credentials
]);
$dynamodb = $sdk->createDynamoDb();
$marshaler = new Marshaler();
$ddb_query_predicate = '{":id": "' . $_GET["submission_id"] . '"}';
$eav = $marshaler->marshalJson($ddb_query_predicate);
$params = [
'TableName' => "Orders",
'KeyConditionExpression' => '#submissionId = :id',
'ExpressionAttributeNames'=> ["#submissionId" => "submissionId"],
'ExpressionAttributeValues' => $eav
];
try {
$submissionQueryResponse = $dynamodb->query($params);
$submission = $submissionQueryResponse['Items'][0];
$submissionObject = $marshaler->unmarshalItem($submission, true);
// Extract the message sent to the ERP system by the preprocessor
$submissionMessageSent = $submissionObject->submissionMessageSent;
echo "<p><b>1:</b> $submissionMessageSent</p>";
echo <<<EOT
<textarea id="sent" wrap="off" placeholder="Sent" rows="20" cols="75">$submissionMessageSent</textarea>
EOT;
} catch (DynamoDbException $e) {
echo "Unable to query:\n";
echo $e->getMessage() . "\n";
}
}
?>
</body>
</html>
NOTE: Complete disclosure. I had to pull out portions of the page's code to not disclose sensitive information. Don't crucify me if I missed a tag some place in the purge. You get the gist of my problem.
Your XML is rendered as HTML, they are ignored by the webbrowser. It possible to see them within the textarea as this will escape the tag's characters.
The tags will be there when viewing the raw html output.

Reading Zillow XML File with PHP

I have an XML file I want to read, but can't seem to make sense of it.
To access the file I pulled you can type in:
http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=X1-ZWz1gyo1562s5n_6sext&address=155+Demar+Blvd&citystatezip=Canonsburg%2C+PA
I'm doing this through PHP, so my code looks like:
<html>
<head>
<title>Hellow World</title>
</head>
<body>
<?php
$zillow_id = 'X1-ZWz1gyo1562s5n_6sext';
$search = '155 Demar Blvd';
$citystate = 'Canonsburg PA';
$address = urlencode($search);
$citystatezip = urlencode($citystate);
$url = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=$zillow_id&address=$address&citystatezip=$citystatezip";
$result = file_get_contents($url);
$data = simplexml_load_string($result);
echo $data->response->results->result->lotSizeSqFt . "<br>";
?>
</body>
</html>
I had expected this code to result in the printout of the lot size - in square feet - to the screen. No dice.
However, the line of code:
echo $data->response->results->result->zpid . "<br>";
returns the expected value for the zpid parameter: 49785503.
In an ideal world the line of code:
echo $data->response->results->result->lotSizeSqFt . "<br>";
would return: 9000.
What am I doing wrong?
You're using the wrong endpoint in your code.
Your endpoint:
http://www.zillow.com/webservice/GetSearchResults.htm
The correct endpoint:
http://www.zillow.com/webservice/GetDeepSearchResults.htm
Using the GetDeepSearchResults will return the results you're looking for.

Pulling nested variable from json API

I'm trying to pull the highlighted variable from this API:
{
"timestamp":{
"total":1486424886,
"exchanges":{
"NEG":1486423855,
"MBT":1486424738,
"LOC":1486422237,
"FOX":1486424483,
"FLW":1486411044,
"B2U":1486424811,
"ARN":1486405596
}
},
"ticker_24h":{
"total":{
"last":**3011.8756088755**, // <---
"high":4073.32,
"low":2631.58,
...
http://api.bitvalor.com/v1/ticker.json
This is my code so far:
<html>
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<table width="auto">
<tr><td>BTC/BRL (Bitvalor)</tr></td>
<tr><td>
<?php
$url1 = "http://api.bitvalor.com/v1/order_book_stats.json";
$fgc1 = file_get_contents($url1);
$json1 = json_decode($fgc1, true);
$price1 = $json1["ticker_24h.total.last"];
echo $price1;
?>
</tr></td>
</table>
</html>
What am I missing?
You access decoded JSON like an associative array:
$price1 = $json1['ticker_24h']['total']['last'];
Make sure you use an isset in case the format of data changes or the response is not what you expect.

Make PHP Click Link On Same PAge Or Process A Form

Is there any php code i can use to click a link or process a form on the page the php is on?
Im building a redirect script and what i need to do is use php to move user to next page, its mandatory that php has to be used html doesnt work. In it i have a self submit forum but it doesnt work how i load the script. Is there a way i can use php code to submit it? or remove it and put a link there then use php to click that link?
This is the code below:
if ($_GET['ref_spoof'] != NULL)
{
$offer = urldecode($_GET['ref_spoof']);
$p1 = strpos ($offer, '?') + 1;
$url_par = substr ($offer , $p1);
$paryval = split ('&', $url_par);
$p = array();
foreach ($paryval as $value)
{
$p[] = split ('=',$value);
}
//header('Location: '.$offer.'') ;
print
'
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">$("#mylink").click()</script>
Index Page
<script type="text/javascript">$("#mylink").click()</script>
<script type="text/javascript">document.getElementById("myLink").click();</script>
<form action="'.$offer.'" method="get" id="myform">
';
foreach ($p as $value)
{
echo '<input type="hidden" name="'.$value[0].'" value="'.$value[1].'">';
}
echo '</form><script language="JavaScript"> document.getElementById(\'myform\').submit();</script></body></html>';
}
Looks like you're trying to make this too complicated.
You're loading a page that submits a form using GET.
Is there any reason you can't use
header("Location : ".$offer."?".http_build_query($p));
http_build_query being a function to generate an URL string from an array. Assuming $p is the array containing all form fieldnames+values.
example of http_build_query:
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data);
will result in:
foo=bar&baz=boom&cow=milk&php=hypertext+processor

Call a php file in webroot from a controller using cake php

I have a php file in webroot.which returns an array of filenames.So I want to call this php file from a controller.can u give me an ideaa how ll I do this...??the content of this php file is given below:
<?php
//if($_SERVER['REMOTE_ADDR']=='10.1.31.77'){debugbreak();}
include ("../vendors/xmlrpc.inc");
?>
<html>
<head>
<title>XML-RPC PHP Demo</title>
</head>
<body>
<h1>XML-RPC PHP Demo</h1>
<?php
$client = new xmlrpc_client('/individual-trade-server-manager/list-binaries-on-trade-server.php',
'125.20.11.245', 80);
//$client->return_type = 'phpvals';
//$client->setDebug ( 2 );
$stringToEcho = 'Hello World';
// Send a message to the server.
$message = new xmlrpcmsg('rpc.FnListAllBinaryFiles',array(php_xmlrpc_encode ( $stringToEcho )));
$result = $client->send($message);
// Process the response.
if (!$result) {
print "<p>Could not connect to HTTP server.</p>";
} elseif ($result->faultCode()) {
print "<p>XML-RPC Fault #" . $result->faultCode() . ": " .
$result->faultString();
} else {
$output=php_xmlrpc_decode($result->value());
$output=explode('*',$output);
//echo "<pre>";
// print_r($output);
}
//echo "<pre>";
// print_r($output);
?>
<table>
<tr><th>Binary Filenames</th></tr>
<?php
foreach($output as $val)
{
//echo "<pre>";
//print_r($val);
?>
<tr><td><?php echo $val; ?></td></tr>
<?php
}
?>
</table>
</body></html>
So i just want call this page from a controller so how ll i call this page,and set this this $output array in the controller.
thanks in advance
I would suggest you create a class with static function in the vendors folder.
The static function will return your array and you will be able to invoke the method from your controller using Myclass::mymethod();

Categories