Warning: implode(): Invalid arguments passed at load page - php

My script load a title of url when this load by php_self in a html form.
<?php
$bk_url=$_REQUEST['bk_url'];
$remote_url = $bk_url;
$from_remote_url = implode("", file("".$remote_url));
if(preg_match("/<title>(.+)<\/title>/", $from_remote_url, $regs)) {
} else {
echo "<br> Title empty. Manual insert";
}
?>
When load the php page, are displayed two errors:
Warning: file(http://): failed to open stream: operation failed
and this error:
Warning: implode(): Invalid arguments passed
I have try to find a solution on stackoverflow but not find any solution.
How to fix this two errors?
Thanks

You need to run the code only when the user has submitted the form. So check first to see if the variable is set.
if (isset($_REQUEST['bk_url'])) {
$bk_url=$_REQUEST['bk_url'];
$remote_url = $bk_url;
$from_remote_url = implode("", file("".$remote_url));
if(preg_match("/<title>(.+)<\/title>/", $from_remote_url, $regs)) {
} else {
echo "<br> Title empty. Manual insert";
}
}

Related

How to handle get_headers() function if URL doesn't exist

While usign the function get_headers() for example, If i wrote
get_headers("www.websitedoesntexist.com");
i get the following error
Warning: get_headers(): php_network_getaddresses: getaddrinfo failed:
No such host is known Warning:
get_headers(www.websitedoesntexist.com): failed to open stream:
php_network_getaddresses: getaddrinfo failed: No such host is known
I want to know how to handle these problems, Something like
if (isset(get_headers("www.websitedoesntexist.com"))) {
echo "URL Exists";
}
Suppress errors on the get_headers() function. Assign get_headers to a variable. Then check the Boolean value of that variable.
$file_headers = #get_headers("www.websitedoesntexist.com");
if(!$file_headers) {
$exists = false;
}
else {
$exists = true;
}
Edit:
Based on the comments, for a better, long-term solution using curl see https://stackoverflow.com/a/36743670/4374801
From the PHP manual :
Returns an indexed or associative array with the headers, or FALSE on failure.
Which means you can test against FALSE.
Read more : http://php.net/manual/en/function.get-headers.php
Edit
If you want just to check if the URL exists or not, instead of supressing the errors I suggest you use curl, a simple function like this can do the job :
function url_exists($url) {
if (!$fp = curl_init($url)) {
return false;
}
return true;
}
if you want to check the url exist or not in php, simply code this.testing purpose
$array = get_headers('url');
$string = $array[0];
if (strpos($string, "200")) {
echo "thats cool dude";
} else {
echo "Bad request";
}

Warning: Missing argument 1 for Drupal\custom_forms\Service\CustomFormsService::__construct() in Drupal forms

I receiving the following warning in my code.
Warning: Missing argument 1 for
Drupal\custom_forms\Service\CustomFormsService::__construct(), called
in
/drupal/docroot/core/lib/Drupal/Component/DependencyInjection/Container.php
on line 264 and defined in
Drupal\custom_forms\Service\CustomFormsService->__construct() (line 13
of modules/custom_forms/src/Service/CustomFormsService.php).
And my code is
public function __construct($request) {
$this->cityname = \Drupal::request()->get('cityname');
if($this->cityname != ''){
$result = \Drupal::database()->select('city_municipality','cm')
->fields('cm',array('municipality'))
->condition('city', $this->cityname)
->execute()->fetchAll();
$result = json_encode($result);
print $result;
exit;
}
}
The forms works fine but the warning message shows...Can anyone help me out

Trouble with calling Namecheap's API?

I used this tutorial to make a simple PHP file that uses Namecheap's API to check the availability of domain names. I followed the tutorial exactly, so my code is:
<?PHP
$command = 'https://api.sandbox.namecheap.com/xml.response?ApiUser=myusername&ApiKey=myapikeyed&UserName=myusername&Command=namecheap.domains.check&ClientIp='.$_SERVER['REMOTE_ADDR'].'&DomainList=adminpeak.com,kavin12345.com';
$data = file_get_contents($command);
$xml = simplexml_load_string($data);
if( ! $xml)
{
// Code to do when load failed.
echo 'unable to load XML file';
}
else
{
// Code to do when load successful.
foreach ($xml->CommandResponse->DomainCheckResult as $result) {
echo $result['Domain'].':'. $result['Available'].'<br />';
}
}
?>
However, when I run this in my terminal using $ php namecheap.php, I get the error message:
Warning: Invalid argument supplied for foreach() in /Users/MyName/Documents/NamecheapPHP/namecheap.php on line 14
Any ideas?

Find uptime of server using ssh in php

I want to find the load average of the server to which am connected full time, I used the following code, but I get error on displaying the up time. please some one help me fix this...
Here is the code
<?php
$ip = '192.168.71.56';
$user = 'viju';
$pass = 'viju';
$connection = ssh2_connect($ip);
if($connection)
{
echo "connection successful";
}
else
{
echo "connection failed";
}
if(!(ssh2_auth_password($connection,$user,$pass)))
{
echo "Authentication Failed";
}
else
{
echo "Authentication Successful";
$shell = ssh2_shell($connection,"uptime");
if($shell)
{
$loadAvgString=explode('average:',$shell); //line 26
print_r($loadAvgString);
$loadAvgInAnArray=explode(',',$loadAvgString);
print_r($loadAvgInAnArray);
}
else
{
echo "No data fetched";
}
}
?>
I get the following output with Warning
connection successful Authentication Successful
Warning: explode() expects parameter 2 to be string, resource given in /var/www/extra/dis.php on line 26 Array ( [0] => )
The second parameter for explode must be a string joined using the string delimiter.just print that $shell variable and check whether it holds a string or not
You should check if $shell contains something rather than whether or not it is true
if (!empty($shell)) {
...
}
even better
if (strstr($shell, "average:")) {
...
}

No json data being returned, when URL contains data

I built a very basic webAPI that when called prints some json data on the screen.
I'm calling the api with the following
function getEnvironmentList(){
$fullUrl = "localhost/serverList/api/rest.php?action=allenvironments&format=json";
$jsonDataRaw = file_get_contents($fullUrl);
return $jsonDataRaw;
}
$jsonData = getEnvironmentList();
echo "<PRE>";
var_dump(json_decode($jsonData, true));
echo "</PRE>";
I get the error Warning: file_get_contents(localhost/serverList/api/rest.php?action=allenvironments&format=json): failed to open stream: No error in C:\path\inc\getJSONdata.php on line 6
Yet when I visit that URL I see this
{"1":{"environmentID":"1","envName":"UAT","envCreatedBy":"mhopkins","envCreatedDtTm":"2013-06-30 00:34:57","envUpdatedBy":"mhopkins","envUpdatedDtTm":"2013-06-30 00:34:57"},"3":{"environmentID":"3","envName":"Platinum","envCreatedBy":"mhopkins","envCreatedDtTm":"2013-06-30 00:37:38","envUpdatedBy":"phpsense","envUpdatedDtTm":"2013-06-30 00:37:38"}}
I'm really confused why the code can't seem to realize there is json data there...
You forgot the http.
$fullUrl = "http://localhost/serverList/api/rest.php?action=allenvironments&format=json";

Categories