I've seen PHP code like this, but I've not found anything that explains it:
$xml = new SimpleXMLElement($url, null, true);
What do null and true do? Is there a way to check to see if the requested $url failed? Please provide an PHP example of the last two options being used. Thanks!
The signature of SimpleXMLElement::_construct is
final public SimpleXMLElement::__construct() ( string $data [, int
$options = 0 [, bool $data_is_url = false [, string $ns = "" [, bool
$is_prefix = false ]]]] )
The default value of third parameter is false, so if you want to set it to true, you will also need to provide the second parameter.
Example:
// Create a SimpleXMLElement object from a URL
$sxe = new SimpleXMLElement('http://example.org/document.xml', null, true);
echo $sxe->asXML();
Related
I m using google site verification recaptcha API for my website.
$json = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response."&remoteip=".$ip);
when I print echo $json; its showing proper response
{ "success": true, "challenge_ts": "2018-08-23T12:43:42Z", "hostname": "staging.my.com" }
but when i tried
$data = json_decode($json,true);
echo $data->success;
its showing nothing
Could anybody tell me what i am missing??
$json = '{ "success": true, "challenge_ts": "2018-08-23T12:43:42Z", "hostname": "staging.my.com" }';
$data = json_decode($json,true);
This produces an associative array from your example JSON string, not an object (used var_dump($data); to see what you actually have stored).
Just use the proper syntax for accessing array values:
echo $data["success"]; // prints '1'
or:
echo ($data["success"])?'success':'failure'; // prints 'success'
According to PHP doc if you set the second parameter assoc to true the function returns associative arrays instead of std class.
mixed json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )
assoc
When TRUE, returned objects will be converted into associative arrays.
So either try $data['success'] or change json_decode($json, true) to json_decode($json).
From the PHP's setcookie documentation the function signature is
bool setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = "" [, bool $secure = false [, bool $httponly = false ]]]]]] )
If I only want to set the $name, $value and $httponly params, and allow the defaults to take effect for the other params. will I still have to lookup the default values for all the other optional params and set them in order to "reach" the $httponly param? e.g:
<?php
setcookie('double_track', '1234567890', 0, '', '', false, true);
^ ^ ^ ^
(do I have to look
up and specify these
values?)
Is there any way around this?
Native PHP methods are specified like that and you can do nothing about it.
However, you can specify your own wrapper method around the native one:
function setMyCookie($name, $value, $httpOnly)
{
setcookie($name, $value, 0, '', '', false, $httpOnly);
// ^ ^ ^ ^
// (specify default values)
}
Now, you may just use setMyCookie as this:
setMyCookie('double_track', '1234567890', true);
I am creating a cookie from this URL "/component/user" in this way:
setcookie("isLogged", $options['user']->name);
I know it will be removed when the browser close, but doing in this way the cookie will be set up only for the "/component/user" URL instead the whole website, so I did this other one with the intention to make it work for whole the website:
setcookie("isLogged", $options['user']->name, "", "/");
but the cookie is not created, what I need is a cookie that expire when the browser close and also set it up for whole website. What am I doing wrong?
From the docs:
bool setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = "" [, bool $secure = false [, bool $httponly = false ]]]]]] )
Use $expire = 0, as it's a timestamp not a string
setcookie("isLogged", $options['user']->name, 0, "/");
Is it possible to set the path and domain one time at the beginning of the script and have all future sets conform to the same?
From the docs:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
sure:
function own_setcookie($name, $value, $expire=0) {
setcookie($name, $value , $expire, 'myPath', 'myDomain');
}
then use:
own_setcookie('myName', 'myValue');
While looking at someone's code today, I saw this:
public $setCookieFunction = 'setcookie';
$setcookie = $this->setCookieFunction;
Anyone knows how this works?
Thanks!
It's likely invoked like this, with the same parameters as setcookie():
$setcookie( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] );
Or:
$this->$setCookieFunction( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] );
The benefit here is that you can change $this->setcookie to whatever function name you want, and that function will get called across the class.