I am trying to write a custom Laravel function on boot, but I get errors












0














So I am trying to write the following function in the App Service Provider but am getting errors:



My code is:



public function boot()
{
$homepage = 'https://example.com';
$already_crawled = ;
$crawling = ;

function follow_links($url)
{
global $already_crawled;
global $crawling;

$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $already_crawled)) {
$already_crawled = $full_link;
$crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($crawling);
foreach ($crawling as $link) {
follow_links($link);
}
}

follow_links($homepage);
}


So with this code I am getting errors like:



in_array() expected parameter 2 to be array, null given



What should I do to run this without problems?










share|improve this question
























  • What version of PHP are you using?
    – Ross Wilson
    Nov 20 at 20:59










  • I am using PHP 7.1
    – Emmanuel-Ab
    Nov 20 at 20:59






  • 1




    What's your end goal with this code i.e. after you've logged the links what are you planning on doing with them?
    – Ross Wilson
    Nov 20 at 21:13










  • I am planning to get data out of them and store it in a database.
    – Emmanuel-Ab
    Nov 20 at 22:04
















0














So I am trying to write the following function in the App Service Provider but am getting errors:



My code is:



public function boot()
{
$homepage = 'https://example.com';
$already_crawled = ;
$crawling = ;

function follow_links($url)
{
global $already_crawled;
global $crawling;

$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $already_crawled)) {
$already_crawled = $full_link;
$crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($crawling);
foreach ($crawling as $link) {
follow_links($link);
}
}

follow_links($homepage);
}


So with this code I am getting errors like:



in_array() expected parameter 2 to be array, null given



What should I do to run this without problems?










share|improve this question
























  • What version of PHP are you using?
    – Ross Wilson
    Nov 20 at 20:59










  • I am using PHP 7.1
    – Emmanuel-Ab
    Nov 20 at 20:59






  • 1




    What's your end goal with this code i.e. after you've logged the links what are you planning on doing with them?
    – Ross Wilson
    Nov 20 at 21:13










  • I am planning to get data out of them and store it in a database.
    – Emmanuel-Ab
    Nov 20 at 22:04














0












0








0







So I am trying to write the following function in the App Service Provider but am getting errors:



My code is:



public function boot()
{
$homepage = 'https://example.com';
$already_crawled = ;
$crawling = ;

function follow_links($url)
{
global $already_crawled;
global $crawling;

$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $already_crawled)) {
$already_crawled = $full_link;
$crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($crawling);
foreach ($crawling as $link) {
follow_links($link);
}
}

follow_links($homepage);
}


So with this code I am getting errors like:



in_array() expected parameter 2 to be array, null given



What should I do to run this without problems?










share|improve this question















So I am trying to write the following function in the App Service Provider but am getting errors:



My code is:



public function boot()
{
$homepage = 'https://example.com';
$already_crawled = ;
$crawling = ;

function follow_links($url)
{
global $already_crawled;
global $crawling;

$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $already_crawled)) {
$already_crawled = $full_link;
$crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($crawling);
foreach ($crawling as $link) {
follow_links($link);
}
}

follow_links($homepage);
}


So with this code I am getting errors like:



in_array() expected parameter 2 to be array, null given



What should I do to run this without problems?







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 20:58









Ross Wilson

15.4k22539




15.4k22539










asked Nov 20 at 20:24









Emmanuel-Ab

257




257












  • What version of PHP are you using?
    – Ross Wilson
    Nov 20 at 20:59










  • I am using PHP 7.1
    – Emmanuel-Ab
    Nov 20 at 20:59






  • 1




    What's your end goal with this code i.e. after you've logged the links what are you planning on doing with them?
    – Ross Wilson
    Nov 20 at 21:13










  • I am planning to get data out of them and store it in a database.
    – Emmanuel-Ab
    Nov 20 at 22:04


















  • What version of PHP are you using?
    – Ross Wilson
    Nov 20 at 20:59










  • I am using PHP 7.1
    – Emmanuel-Ab
    Nov 20 at 20:59






  • 1




    What's your end goal with this code i.e. after you've logged the links what are you planning on doing with them?
    – Ross Wilson
    Nov 20 at 21:13










  • I am planning to get data out of them and store it in a database.
    – Emmanuel-Ab
    Nov 20 at 22:04
















What version of PHP are you using?
– Ross Wilson
Nov 20 at 20:59




What version of PHP are you using?
– Ross Wilson
Nov 20 at 20:59












I am using PHP 7.1
– Emmanuel-Ab
Nov 20 at 20:59




I am using PHP 7.1
– Emmanuel-Ab
Nov 20 at 20:59




1




1




What's your end goal with this code i.e. after you've logged the links what are you planning on doing with them?
– Ross Wilson
Nov 20 at 21:13




What's your end goal with this code i.e. after you've logged the links what are you planning on doing with them?
– Ross Wilson
Nov 20 at 21:13












I am planning to get data out of them and store it in a database.
– Emmanuel-Ab
Nov 20 at 22:04




I am planning to get data out of them and store it in a database.
– Emmanuel-Ab
Nov 20 at 22:04












1 Answer
1






active

oldest

votes


















1














Your variables in your boot function aren't global, so your follow_links function's globals are a totally separate set of variables. You should basically never have the keyword global anywhere in Laravel, ever.



Because of your scope issues, $already_crawled is undefined when you first attempt to feed it to is_array. Use class properties, and $this to access them. On top of that, I've removed the weird function-in-a-function construction:



protected $already_crawled;
protected $crawling;
protected $homepage;

public function boot()
{
$this->homepage = 'https://example.com';
$this->already_crawled = ;
$this->crawling = ;

$this->follow_links($this->homepage);
}

protected function follow_links($url)
{
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $this->already_crawled)) {
$this->already_crawled = $full_link;
$this->crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($this->crawling);
foreach ($this->crawling as $link) {
$this->follow_links($link);
}
}


Side note: You almost certainly don't want this in your service provider. It's going to make a HTTP file_get_contents call on every single pageview your app ever serves. It's going to slow your app down dramatically.






share|improve this answer





















  • Can you tell me a better way to set this piece of code in my Laravel project? I really don't know here to put it and my "best" solution was to set it up in the boot function.
    – Emmanuel-Ab
    Nov 20 at 21:11










  • @Emmanuel-Ab It depends on why you're running this code. I can't really figure out what the point of this crawling is; you don't save the crawled data anywhere or really do anything with it at all.
    – ceejayoz
    Nov 20 at 21:18










  • The code is not yet complete. I will store data from the crawled urls in a database.
    – Emmanuel-Ab
    Nov 20 at 22:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400978%2fi-am-trying-to-write-a-custom-laravel-function-on-boot-but-i-get-errors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your variables in your boot function aren't global, so your follow_links function's globals are a totally separate set of variables. You should basically never have the keyword global anywhere in Laravel, ever.



Because of your scope issues, $already_crawled is undefined when you first attempt to feed it to is_array. Use class properties, and $this to access them. On top of that, I've removed the weird function-in-a-function construction:



protected $already_crawled;
protected $crawling;
protected $homepage;

public function boot()
{
$this->homepage = 'https://example.com';
$this->already_crawled = ;
$this->crawling = ;

$this->follow_links($this->homepage);
}

protected function follow_links($url)
{
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $this->already_crawled)) {
$this->already_crawled = $full_link;
$this->crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($this->crawling);
foreach ($this->crawling as $link) {
$this->follow_links($link);
}
}


Side note: You almost certainly don't want this in your service provider. It's going to make a HTTP file_get_contents call on every single pageview your app ever serves. It's going to slow your app down dramatically.






share|improve this answer





















  • Can you tell me a better way to set this piece of code in my Laravel project? I really don't know here to put it and my "best" solution was to set it up in the boot function.
    – Emmanuel-Ab
    Nov 20 at 21:11










  • @Emmanuel-Ab It depends on why you're running this code. I can't really figure out what the point of this crawling is; you don't save the crawled data anywhere or really do anything with it at all.
    – ceejayoz
    Nov 20 at 21:18










  • The code is not yet complete. I will store data from the crawled urls in a database.
    – Emmanuel-Ab
    Nov 20 at 22:03
















1














Your variables in your boot function aren't global, so your follow_links function's globals are a totally separate set of variables. You should basically never have the keyword global anywhere in Laravel, ever.



Because of your scope issues, $already_crawled is undefined when you first attempt to feed it to is_array. Use class properties, and $this to access them. On top of that, I've removed the weird function-in-a-function construction:



protected $already_crawled;
protected $crawling;
protected $homepage;

public function boot()
{
$this->homepage = 'https://example.com';
$this->already_crawled = ;
$this->crawling = ;

$this->follow_links($this->homepage);
}

protected function follow_links($url)
{
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $this->already_crawled)) {
$this->already_crawled = $full_link;
$this->crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($this->crawling);
foreach ($this->crawling as $link) {
$this->follow_links($link);
}
}


Side note: You almost certainly don't want this in your service provider. It's going to make a HTTP file_get_contents call on every single pageview your app ever serves. It's going to slow your app down dramatically.






share|improve this answer





















  • Can you tell me a better way to set this piece of code in my Laravel project? I really don't know here to put it and my "best" solution was to set it up in the boot function.
    – Emmanuel-Ab
    Nov 20 at 21:11










  • @Emmanuel-Ab It depends on why you're running this code. I can't really figure out what the point of this crawling is; you don't save the crawled data anywhere or really do anything with it at all.
    – ceejayoz
    Nov 20 at 21:18










  • The code is not yet complete. I will store data from the crawled urls in a database.
    – Emmanuel-Ab
    Nov 20 at 22:03














1












1








1






Your variables in your boot function aren't global, so your follow_links function's globals are a totally separate set of variables. You should basically never have the keyword global anywhere in Laravel, ever.



Because of your scope issues, $already_crawled is undefined when you first attempt to feed it to is_array. Use class properties, and $this to access them. On top of that, I've removed the weird function-in-a-function construction:



protected $already_crawled;
protected $crawling;
protected $homepage;

public function boot()
{
$this->homepage = 'https://example.com';
$this->already_crawled = ;
$this->crawling = ;

$this->follow_links($this->homepage);
}

protected function follow_links($url)
{
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $this->already_crawled)) {
$this->already_crawled = $full_link;
$this->crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($this->crawling);
foreach ($this->crawling as $link) {
$this->follow_links($link);
}
}


Side note: You almost certainly don't want this in your service provider. It's going to make a HTTP file_get_contents call on every single pageview your app ever serves. It's going to slow your app down dramatically.






share|improve this answer












Your variables in your boot function aren't global, so your follow_links function's globals are a totally separate set of variables. You should basically never have the keyword global anywhere in Laravel, ever.



Because of your scope issues, $already_crawled is undefined when you first attempt to feed it to is_array. Use class properties, and $this to access them. On top of that, I've removed the weird function-in-a-function construction:



protected $already_crawled;
protected $crawling;
protected $homepage;

public function boot()
{
$this->homepage = 'https://example.com';
$this->already_crawled = ;
$this->crawling = ;

$this->follow_links($this->homepage);
}

protected function follow_links($url)
{
$doc = new DOMDocument();
$doc->loadHTML(file_get_contents($url));

$linklist = $doc->getElementsByTagName('a');

foreach ($linklist as $link) {
$l = $link->getAttribute("href");
$full_link = 'https://example.com' . $l;

if (!in_array($full_link, $this->already_crawled)) {
$this->already_crawled = $full_link;
$this->crawling = $full_link;
Log::info($full_link . PHP_EOL);
}
}

array_shift($this->crawling);
foreach ($this->crawling as $link) {
$this->follow_links($link);
}
}


Side note: You almost certainly don't want this in your service provider. It's going to make a HTTP file_get_contents call on every single pageview your app ever serves. It's going to slow your app down dramatically.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 21:04









ceejayoz

140k34215299




140k34215299












  • Can you tell me a better way to set this piece of code in my Laravel project? I really don't know here to put it and my "best" solution was to set it up in the boot function.
    – Emmanuel-Ab
    Nov 20 at 21:11










  • @Emmanuel-Ab It depends on why you're running this code. I can't really figure out what the point of this crawling is; you don't save the crawled data anywhere or really do anything with it at all.
    – ceejayoz
    Nov 20 at 21:18










  • The code is not yet complete. I will store data from the crawled urls in a database.
    – Emmanuel-Ab
    Nov 20 at 22:03


















  • Can you tell me a better way to set this piece of code in my Laravel project? I really don't know here to put it and my "best" solution was to set it up in the boot function.
    – Emmanuel-Ab
    Nov 20 at 21:11










  • @Emmanuel-Ab It depends on why you're running this code. I can't really figure out what the point of this crawling is; you don't save the crawled data anywhere or really do anything with it at all.
    – ceejayoz
    Nov 20 at 21:18










  • The code is not yet complete. I will store data from the crawled urls in a database.
    – Emmanuel-Ab
    Nov 20 at 22:03
















Can you tell me a better way to set this piece of code in my Laravel project? I really don't know here to put it and my "best" solution was to set it up in the boot function.
– Emmanuel-Ab
Nov 20 at 21:11




Can you tell me a better way to set this piece of code in my Laravel project? I really don't know here to put it and my "best" solution was to set it up in the boot function.
– Emmanuel-Ab
Nov 20 at 21:11












@Emmanuel-Ab It depends on why you're running this code. I can't really figure out what the point of this crawling is; you don't save the crawled data anywhere or really do anything with it at all.
– ceejayoz
Nov 20 at 21:18




@Emmanuel-Ab It depends on why you're running this code. I can't really figure out what the point of this crawling is; you don't save the crawled data anywhere or really do anything with it at all.
– ceejayoz
Nov 20 at 21:18












The code is not yet complete. I will store data from the crawled urls in a database.
– Emmanuel-Ab
Nov 20 at 22:03




The code is not yet complete. I will store data from the crawled urls in a database.
– Emmanuel-Ab
Nov 20 at 22:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400978%2fi-am-trying-to-write-a-custom-laravel-function-on-boot-but-i-get-errors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'