I’m trying to figure out how to echo a variable inside a DEFINE
function in my php page.
I get the variable like so:
$arr=file("myFile.txt");
foreach($arr as $str){
list($token)=explode("|",$str);
}
and then I echo it like so on the same page:
echo $token;
up to this point everything works fine.
but I need to echo the $token
inside a DEFINE
on the same page like so:
DEFINE("AUTH_TOKEN", "'".$token."'");
I don’t get any error at all but this doesn’t work.
however, if i use:
DEFINE("AUTH_TOKEN", 'dghsa7dasdbhas8hdasdasod9a999');
it works just fine. the dghsa7dasdbhas8hdasdasod9a999
is the value of $token
stored in the database.
could someone please let me know if I’m missing something or doing anything wrong?
Thanks in advance
EDIT:
This is the entire code I am using:
// eBay site to use - 0 = United States
DEFINE("SITEID", 0);
// production vs. sandbox flag - true=production
DEFINE("FLAG_PRODUCTION", false);
// eBay Trading API version to use
DEFINE("API_COMPATIBILITY_LEVEL", 779);
/* Set the Dev, App and Cert IDs
Create these on developer.ebay.com
check if need to use production or sandbox keys */
$arr = file("myFile.txt");
foreach ($arr as $str) {
list($token) = explode("|", $str);
}
if (FLAG_PRODUCTION) {
// PRODUCTION
// Set the production URL for Trading API
DEFINE("API_URL", 'https://api.ebay.com/ws/api.dll');
// Set the auth token for the user profile used
DEFINE("AUTH_TOKEN", 'YOUR_PRODUCTION_TOKEN');
} else {
$arr = file("myFile.txt");
foreach ($arr as $str) {
list($token) = explode("|", $str);
}
echo $token;
// SANDBOX
// Set the sandbox URL for Trading API calls
DEFINE("API_URL", 'https://api.sandbox.ebay.com/ws/api.dll');
// Set production credentials (from developer.ebay.com)
// Set the auth token for the user profile used
DEFINE("AUTH_TOKEN", "'" . $token . "'");
}
the database is in a .txt file called myFile.txt
and it looks like this:
111111111111111|222222222222222222|3333333333333333333|4444444444444444444444
2
Answers
This code has a number of issues such as the repetition of $arr=file(“myFile.txt”), however the crux of the question.
Move AUTH_TOKEN out of the if/else block and define it just after the foreach($arr as $str) loop (before the if/else)
This way you will be defining the AUTH_TOKEN once only and this will be far easier to debug.
You can try this: