I'm writing a relatively simple if statement to process the response of a web-service
The below code will output the type of data that's being returned and the value of it:
$response = $client->__getLastResponse();
echo $response;
echo gettype($response);
For a correct response, I get 'true string'
For an incorrect response, I get 'false string'
So I need an if statement to process whether it's true or false:
if($response == "true"){
echo "Logged In";
} else {
echo "Not Logged In";
}
Whatever response I get, I always receive "Not Logged In"
I've tried the === operator also, but to no avail.
Can someone help?
For a correct response, I get 'true string'
For an incorrect response, I get 'false string'
Provided that's you actual code, you have echo $response . gettype( $response ) which yields "true string", right? If so, there's a space after "true". Try to trim the response:
<?php
$response = $client->__getLastResponse();
if( strtolower( trim( $response ) ) === 'true' ) {
echo 'response is true.';
}
Apart from that, debugging is really easy using var_dump, as that will display quotes around the value and the total length of the string, making the additional space pop-up much easier to spot.
Related
I'm creating a PHP page that uses an if statement to compare the value of a shell script to a string, but it alway seems to be false:
<?php
$output = shell_exec('/usr/sbin/genarts_server_status');
echo $output
if($output == "UP")
{
echo 'Server is up';
} else {
echo 'Server is down';
}
?>
Line 3 is simply echoing the value of the variable and that echo does correctly return "UP", but the comparison of the variable to the "UP" string returns false and therefore echoes "Server is down".
Any ideas what I am doing wrong?
SOLVED!! Sorry for wasting your time.
Problems:
Second function "verify_webhook_2" always returns false.
Code in if statement runs whether tests return true or not.
I copied and pasted the first function, then made (what I would think to be) appropriate changes, so I can verify webhooks coming from two different Shopify stores. I'm sure it's something simple that I am just oblivious to, as I'm still fairly new to all of this. If I change $verify to the secret for $verify2 then webhooks received from that shop will verify true.
And I cannot for the life of me understand why the code in the if statement runs even when both requirements test false. There's no way I can think of that either could prove true when the receiving a webhook from the shop related to the $verify2 secret. Probably a rookie mistake?
$verify = "xxxxsecretxxxx";
$verify2 = "xxxxsecretxxxx";
define('SHOPIFY_APP_SECRET', $verify);
define('SHOPIFY_APP_SECRET_2', $verify2);
function verify_webhook($data, $hmac_header)
{
$calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true));
return hash_equals($hmac_header, $calculated_hmac);
}
function verify_webhook_2($data, $hmac_header)
{
$calculated_hmac_2 = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET_2, true));
return hash_equals($hmac_header, $calculated_hmac_2);
}
$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
$verified_2 = verify_webhook_2($data, $hmac_header);
error_log('Webhook verified: '.var_export($verified, true)); //check error.log to see the result
if ($verified == true || $verified_2 == true){
header("HTTP/1.1 200 OK"); //respond with success
http_response_code(201); //respond with success
file_put_contents('/var/www/html/temp/webhook.json', $data);
$POST = json_decode(file_get_contents('/var/www/html/temp/webhook.json'), true);
//$POST = $POST['id'];
$report = "id: " . $POST['id'] . " - email: " . $POST['email'] . " - name: " . $POST['customer']['first_name'] . " " . $POST['customer']['last_name'] ;
}else{
}
Of course, right after posting the question, I realized my failure. I had only written to the error log for the first function's comparison, so when I kept seeing "webhook verified: false" in the error logs, I assumed that was regardless of the shop I was sending data from.
I added:
error_log('Webhook verified_2: '.var_export($verified_2, true)); //check error.log to see the result
just below the first error_log call, then added another error log into the else section of my if statement, and all is working correctly, and responding correctly.
It was a lack of understanding on my part that led to me believing it was not working correctly, when in fact, everything was, but I was missing information.
I want to receive a POST request from a JS client with a json body (i.e. this is not form data), and save the .gigs (javascript) array to a file, after checking the .password field. This is all my code (based on Receive JSON POST with PHP)
$json_params = file_get_contents("php://input");
if (strlen($json_params) > 0 && isValidJSON($json_params)){
/* json_decode(..., true) returns an 'array', not an 'object'
* Working combination: json_decode($json_params) WITH $inp->password
*/
$inp = json_decode($json_params);
} else {
echo "could not decode POST body";
return;
}
$password = $inp->password;
// echo $password;
if ($password == "****") {
$gigs = $inp['gigs'];
// WAS $res = file_put_contents('gigs.json', json_encode($gigs), TEXT_FILE);
$res = file_put_contents('gigs.json', json_encode($gigs));
if ($res > 0) {
echo "Success";
return;
} else {
if (!$res) {
http_response_code(500);
echo "file_put_contents error:".$res;
return;
} else {
http_response_code(500);
echo "Error: saved zero data!";
return;
}
}
}
else {
// http_response_code(403); // (2)
echo "Password invalid";
return;
}
What I find is that
if I comment out the if statement and uncomment echo $password; then the right password is there
if I uncomment line 2, which I want to do, then I get back a 500 and the error logs refer an Illegal string offset 'password' in line (1) above. Without that I get back a "Success" (all for the same password).
I don't understand what is happening, nor how to get 200, 403 and 500 error messages safely.
Note
$json_params = file_get_contents("php://input");
If your scripts are running upon regular HTTP requests, passing data like it comes from HTML form, them you should consider using $_POST for your content, not php://input. If you expect JSON in request body, then I'd be fine, yet I'd also check content type for application/json.
Next:
$inp = "I never got set";
if (strlen($json_params) > 0 && isValidJSON($json_params)){
$inp = json_decode($json_params, true);
}
$password = $inp->password;
$password = $inp['password'];
This is pretty broken. First, see json_decode() arguments (2nd) -> you are decoding to array (true), not object (false), so only $password = $inp['password']; will work in your case. Also the whole code will fail when your input data is invalid as in that case $np is rubbish string, not the array you try to read later on. Use null as default value and check for that prior further use.
Next:
$res = file_put_contents('gigs.json', json_encode($gigs), FILE_TEXT);
there's no FILE_TEXT option for file_put_contents(). Nor you'd need one.
Once you correct these you'd be fine. Also print_r() and var_dump() may be the functions you wish to get familiar with for your further debugging.
In general http://php.net/ -> lookup for functions you are about to use.
What is wrong with this bit of code. Whether I tick the reCAPTCHA or not, it goes onto the else clause.
<?php
// This is added for Google Captcha
$url = 'https://www.google.com/recaptcha/api/siteverify';
$privatekey = '6LdBjyATAAAAABZe1O-DKBEQnOIzanoVLGEvsvyu';
$response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response);
if($response.success==false){
echo "<h2>Spam Spam go away</h2><p>And if you're not spam, we apologise. Please go back and tick the reCAPTCHA box.</p><p>Thank you</p>";
die();
} else {
// do loads of clever stuff
}
In PHP the dot . operator is for appending strings.
Because the weakly typing of PHP you can append strings to everything.
This line of code:
if($response.success==false){
Would append 'success' to the $response stdClass.
If you enable notices, this would trigger a notice, because of that the string is not inside quotes.
The output string is in that case success, and that is not false in PHP.
What you want, is this:
if($response->success==false){
You need to access it as a property of stdClass.
I'm currently working on a geocoding php function, using google maps API. Strangely, file_get_contents() returns bool(false) whereas the url I use is properly encoded, I think.
In my browser, when I test the code, the page takes a very long time to load, and the geocoding doesn't work (of course, given that the API doesn't give me what I want).
Also I tried to use curl, no success so far.
If anyone could help me, that'd be great !
Thanks a lot.
The code :
function test_geocoding2(){
$addr = "14 Boulevard Vauban, 26000 Valence";
if(!gc_geocode($addr)){
echo "false <br/>";
}
}
function gc_geocode($address){
$address = urlencode($address);
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
$resp_json = file_get_contents($url);
$resp = json_decode($resp_json, true);
if($resp['status']=='OK'){
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
if($lati && $longi){
echo "(" . $lati . ", " . $longi . ")";
}else{
echo "data not complete <br/>";
return false;
}
}else{
echo "status not ok <br/>";
return false;
}
}
UPDATE : The problem was indeed the fact that I was behind a proxy. I tested with another network, and it works properly.
However, your answers about what I return and how I test the success are very nice as well, and will help me to improve the code.
Thanks a lot !
The problem was the fact that I was using a proxy. The code is correct.
To check if there is a proxy between you and the Internet, you must know the infrastructure of your network. If you work from a school or a company network, it is very likely that a proxy is used in order to protect the local network.
If you do not know the answer, ask your network administrator.
If there is no declared proxy in your network, it is still possible that a transparent proxy is there. However, as states the accepted answer to this question: https://superuser.com/questions/505772/how-can-i-find-out-if-there-is-a-proxy-between-myself-and-the-internet-if-there
If it's a transparent proxy, you won't be able to detect it on the client PC.
Some website also provide some proxy detectors, though I have no idea of how relevant is the information given there. Here are two examples :
http://amibehindaproxy.com/
http://www.proxyserverprivacy.com/free-proxy-detector.shtml
When you are not return anything function returns null.
Just use that:
if(!is_null(gc_geocode($addr))) {
echo "false <br/>";
}
Or:
if(gc_geocode($addr) === false) {
echo "false <br/>";
}
Take a look at the if statement:
if(!gc_geocode($addr)){
echo "false <br/>";
}
This means that if gc_geocode($addr) returns either false or null, this statement will echo "false".
However, you never actually return anything from the function, so on success, it's returning null:
$address = urlencode($address);
$url = "http://maps.google.com/maps/api/geocode/json?address={$address}";
$resp_json = file_get_contents($url);
$resp = json_decode($resp_json, true);
if($lati && $longi){
echo "(" . $lati . ", " . $longi . ")"; //ECHO isn't RETURN
/* You should return something here, e.g. return true */
} else {
echo "data not complete <br/>";
return false;
}
} else {
echo "status not ok <br/>";
return false;
}
Alternatively, you can just change the if statement to only fire when the function returns false:
if(gc_geocode($addr)===false){
//...
Above function gc_geocode() working properly on my system, without any extra load. You have called gc_geocode () it returns you lat, long that is correct now you have check through
if(!gc_geocode($addr)){
echo "false <br/>";
}
Use
if($responce=gc_geocode($addr)){
echo $responce;
}
else{
echo "false <br/>";
}