简体   繁体   中英

Passing Argument via Command Line php

So I have this script that I'm trying to pass the argument via command line. But, can't get it to work. Here's the code for the script:

#!/usr/bin/php 

<?php
$argv[1] = $scraper;
require_once('Scraper.php');
$scraper = new JonH_Scraper();

if (empty($argv[1])) {
    echo 'whats the URL?';
    }

$scraper->scrape($url);
var_dump($scraper);

the mistake is here

$argv[1] = $scraper;

it should be

$scraper = $argv[1];

regards

I believe the problem is with this line:

$argv[1] = $scraper;

You assign $scraper to $argv[1] but $scraper is undefined so the assignment becomes null. Later on you check if $argv[1] is empty, which it will be since its NULL .

Perhaps it should be reversed?

$scraper = (isset($argv[1])) ? $argv[1] : null;

if (empty($scraper)) die('No url provided');

EDIT: You may want to use a different variable name since you go ahead and create a new Scraper object and assign it to that same variable.

Should be something like this:

php script.php <URL>


#!/usr/bin/php 
<?php
require_once('Scraper.php'); 
$url = $argv[1];
if(empty($url)){
 die('URL is EMPTY');
}
$scraper = new Scraper();
$results = $scraper->scrape($url);
var_dump($results);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM