Jump to content

API Question. Just learning


Recommended Posts

So i am playing around nothing big just learning so I know not to do a ton of request. This is just mine and just learning. I have some code I am trying to do so I can do calls to the https://api.truckersmp.com/v2/bans/ So I have in a DB someones truckers MP id it is like <?php echo $row['truckermp']; ?> calls for it from my DB. What I wanted to do is have that command about pull the truckers MP ID from the DB and place it in the URL from the calls I have above this. 

Kind of like before So i am wondering If i need to put it in a function since I am learning I am not stressed about this just wanting to figuer it out and then my next move is to have it pull once and have it placed in my own DB so I am not doing calls to truckers mp API much at all. But it doesn't work So i have a users table that has a field of truckermp and well I might be doing this wrong.  I know is I put in https://api.truckersmp.com/v2/bans/24 it works pulling back users 24 details. So how do i code it to pull what I have in the DB. Before you ask I already have all my DB stuff working in other code on the site fine it is just with this one Line wanting to know if anyone could point me the right way or show me where I am wrong.  Thank you all. I am just learning so please don;t hurt me too bad in your comments back. 

 

<div class="container">
				<center><h3> -Truckers MP Punishment Details- </h3>
					<div class="well well-sm" style="width: 80%;">
						<div align="left">
							<?php $apiResponse = json_decode(file_get_contents("https://api.truckersmp.com/v2/bans/<?php echo $row['truckermp']; ?>"), true); ?>
							<?php foreach ($apiResponse['response'] as $row) { ?>
							<?php echo "<b>Time Added : </b>"; echo $row['timeAdded'] ?><br>
							<?php echo "<b>Expiration : </b>"; echo $row['expiration'] ?><br>
							<?php echo "<b>Reason : </b>"; echo $row['reason'] ?><br>
							<?php echo "<b>Admin Name : </b>"; echo $row['adminName'] ?><br>
							<br />
						<?php } ?>
					</div>
				</center>
			</div>

 

spacer.png

Link to comment
Share on other sites

1 hour ago, ClumsyZombie said:

What I wanted to do is have that command about pull the truckers MP ID from the DB and place it in the URL from the calls I have above this. 

First you should count the number of your ids and then iterate (going thorugh one by one) through your ids making an request for each id.

Later you can evaluate each response in a loop or for itself

$tmpids = $row[truckersmp'];
$no_of_tmpids = count($tmpids);

for($i = 0; $i < $no_of_tmpids; $i++)
{
          $apiResponse[$i] = json_decode(file_get_contents("https://api.truckersmp.com/v2/bans/".$tmpids[$i].""), true);                     
}
                               
//
//
for($i = 0; $i < $no_of_tmpids; $i++)
{
//do stuff with each response
}

Q: How can you get those ids from your database?

A: You need to setup a DB Server. Best free method that you can use is mySQL.

For development purposes you can also use XAMPP. Which is a package with several components like a mail server, a classic mySQL DB Server and a FTP- / Webserver

It also inclludes phpmyadmin which is a good program for basic database administration.

 

You need to create a new database.

 

You can just click on "new" http://prntscr.com/dnvs7j

or execute an sql query "CREATE DATABASE name"

 

Then create a new table by filling out the name field and by setting the amount of columns then cofirm with ok http://prntscr.com/dnvsxv

or sql query: CREATE TABLE table_name column1_name column1_def column2_name column2_def

where columnx_def definition for the datatypes (Text, int, varchar etc)

 

Before you can start with mysql interaction, you must establish a connection to the database.

See example below

 

/*

//Setting up an db connection in php (from w3schools.com)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

*/

 

In php you should use mysqli as interface.

If you want to execute a query with mysli, you just call the query function which is mysqli_query("$connection", "$query");

 

When you want to add an id to the table, you can do that by the interface or by mysqli /sql query

INSERT INTO `idsforrequest` (`tmpid`) VALUES (`$truckermp`)

 

When you want to read ids from your database, you can simply do

 

$row = fetch_assoc(mysql_query("$connection", "SELECT * FROM idsforrequest"));

//then evaluate every row

 

or

 

while($row = $query->fetch_array())

{

     $tmpid = $row['truckermp'];   //this is where you read the ids out of the database. WARNING: STRONG CASE SENSIVITY!

     //do stuff with one id in each iteration

}

 

Later when you want to delete those ids from your table, you can do "DELETE FROM database WHERE truckersmp='$number'"

"Just because others break rules, does not grant you the permission to break the rules as well"

Please drive respectful on the servers. Thank you!

 

 

Link to comment
Share on other sites

18 minutes ago, RayRay5 said:

First you should count the number of your ids and then iterate (going thorugh one by one) through your ids making an request for each id.

Later you can evaluate each response in a loop or for itself


$tmpids = $row[truckersmp'];
$no_of_tmpids = count($tmpids);

for($i = 0; $i < $no_of_tmpids; $i++)
{
          $apiResponse[$i] = json_decode(file_get_contents("https://api.truckersmp.com/v2/bans/".$tmpids[$i].""), true);                     
}
                               
//
//
for($i = 0; $i < $no_of_tmpids; $i++)
{
//do stuff with each response
}

Q: How can you get those ids from your database?

A: You need to setup a DB Server. Best free method that you can use is mySQL.

For development purposes you can also use XAMPP. Which is a package with several components like a mail server, a classic mySQL DB Server and a FTP- / Webserver

It also inclludes phpmyadmin which is a good program for basic database administration.

 

You need to create a new database.

 

You can just click on "new" http://prntscr.com/dnvs7j

or execute an sql query "CREATE DATABASE name"

 

Then create a new table by filling out the name field and by setting the amount of columns then cofirm with ok http://prntscr.com/dnvsxv

or sql query: CREATE TABLE table_name column1_name column1_def column2_name column2_def

where columnx_def definition for the datatypes (Text, int, varchar etc)

 

Before you can start with mysql interaction, you must establish a connection to the database.

See example below

 

/*

//Setting up an db connection in php (from w3schools.com)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

*/

 

In php you should use mysqli as interface.

If you want to execute a query with mysli, you just call the query function which is mysqli_query("$connection", "$query");

 

When you want to add an id to the table, you can do that by the interface or by mysqli /sql query

INSERT INTO `idsforrequest` (`tmpid`) VALUES (`$truckermp`)

 

When you want to read ids from your database, you can simply do

 

$row = fetch_assoc(mysql_query("$connection", "SELECT * FROM idsforrequest"));

//then evaluate every row

 

or

 

while($row = $query->fetch_array())

{

     $tmpid = $row['truckermp'];   //this is where you read the ids out of the database. WARNING: STRONG CASE SENSIVITY!

     //do stuff with one id in each iteration

}

 

Later when you want to delete those ids from your table, you can do "DELETE FROM database WHERE truckersmp='$number'"

 

 

Man Thank you so much @RayRay5 I wasn't sure were to just ask for help you provided so much details it is crazy. Thanks figured I was just missing something crazy., It is ok to ask stuff like that here correct? I am just looking to learn and just not a good place to learn as no one wants to show anyone anything because they are scared people are going to steal it. I just want to learn and write my own but have someone show me how they did stuff. Nothing Major I not for sure wanting to use anyone code I want it to be my own. That is why i have been looking at the API's so hard I would like to figure out how to look at hidden API's I know there is tons out there. 

 

Man thanks a ton I tried reaching out to people that have made stuff and they just don't want to talk to me. 

spacer.png

Link to comment
Share on other sites

I wasn't sure where to start, so better do all than stuff that you really don't need.

And sure, this is the right forums, read the desription of it ;) and hopefully you understood everything what I wrote (tbh it was 3am when I wrote my post), otherwise, feel free to ask again.

 

Also thanks to CJ for his correction. I wasn't aware that there is something better than a simple mysqli() for db-connections

 

9 hours ago, ClumsyZombie said:

That is why i have been looking at the API's so hard I would like to figure out how to look at hidden API's I know there is tons out there. 

They are for internal use only. If you can find them then you have to investigate their behavior yourself, or you keep yourself locked out and don't use them.

 

10 hours ago, ClumsyZombie said:

Man thanks a ton I tried reaching out to people that have made stuff and they just don't want to talk to me. 

People who did stuff, usually aren't good teachers as well or they don't have the time or motivation to do it

 

Furthermore, take a look at stackoverflow.com which is a forums specialized on programming questions. There will always be someone able to answer your questions.

"Just because others break rules, does not grant you the permission to break the rules as well"

Please drive respectful on the servers. Thank you!

 

 

Link to comment
Share on other sites

@rayray5

I did the following playing around and what is crazy is it works some times and not others. it is wired. 

 

				<?php
					$truckmp = $row['truckermp'];
					$apiResponse = json_decode(file_get_contents("https://api.truckersmp.com/v2/bans/".$truckmp.""), true);
					function checkUserID($apiResponse) {
						return $apiResponse['error'] == false ? true : false;
					}
				?>
				
				<div class="container">
					<center><h3> -Truckers MP Punishment Details- </h3>
						<div class="well well-sm" style="width: 80%;">
							<div align="left">
								<?php
									if (checkuserID($apiResponse)) {
										foreach ($apiResponse['response'] as $row) {
											echo "<b>Time Added: </b>".$row['timeAdded']."<br>
												  <b>Expiration: </b>".$row['expiration']."<br>
												  <b>Reason: </b>".$row['reason']."<br>
												  <b>Admin Name: </b>".$row['adminName']."<br><br>";
										}
									} else {
										echo $apiResponse['descriptor'];
									}
								?>
							</div>
						</div>
					</center>
				</div>

 

spacer.png

Link to comment
Share on other sites

I think I found the issue. the main one is that I have 2 different DB calls to 2 different tables in this file so it is grabbing the one closer to it and not the one in the top of the file. like it should be. So going to have to figure that part out. It is grabbing the one right above it when the one above it has nothing to do with that. So i need to figure that out to tell it to use the top DB PBO request for the user table and not the jobs table one that is right above it. 

spacer.png

Link to comment
Share on other sites

So I tried something like this below to say hey for the next section of code use the below DB. 

			<?php
			try { $pdo = new PDO('mysql:host=**********.com;dbname=dbname','user','password');

    		$sql = "SELECT *
    		           FROM users
    		           ORDER BY id DESC"
    		           ;

    		$q = $pdo->query($sql);
    		$q->setFetchMode(PDO::FETCH_ASSOC);
			} catch (PDOException $e) {
    		die("Could not connect to the database $dbname :" . $e->getMessage());
			}
			$pdo = null;
			$truckmp = $row['truckermp'];
			$apiResponse = json_decode(file_get_contents("https://api.truckersmp.com/v2/bans/".$truckmp.""), true);
			function checkUser($apiResponse) {
				return $apiResponse['error'] == false ? true : false;
			}
			?>

 

spacer.png

Link to comment
Share on other sites

be aware, you must cache responses from the api when you put this live on the internet, otherwise you risk getting banned from using the API. For testing, don't exceed 30 requests / minute (one request every 2 seconds).

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.