-1

The following PHP code using foreach does not seem to work. I believe it has to do with "<a href='/$value/access'>".

I've shared the entire codebase.

Does anyone know what is wrong with my statement?

// include/functions.php

<?php

    // Defining the basic cURL function
    function curl($url) {
        // Assigning cURL options to an array
        $options = Array(
            CURLOPT_RETURNTRANSFER => TRUE,  // Setting cURL's option to return the webpage data
            CURLOPT_FOLLOWLOCATION => TRUE,  // Setting cURL to follow 'location' HTTP headers
            CURLOPT_AUTOREFERER => TRUE, // Automatically set the referer where following 'location' HTTP headers
            CURLOPT_CONNECTTIMEOUT => 120,   // Setting the amount of time (in seconds) before the request times out
            CURLOPT_TIMEOUT => 120,  // Setting the maximum amount of time for cURL to execute queries
            CURLOPT_MAXREDIRS => 10, // Setting the maximum number of redirections to follow
            CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8",  // Setting the useragent
            CURLOPT_URL => $url, // Setting cURL's URL option with the $url variable passed into the function
        );

        $ch = curl_init();  // Initialising cURL 
        curl_setopt_array($ch, $options);   // Setting cURL's options using the previously assigned array data in $options
        $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
        curl_close($ch);    // Closing cURL 
        return $data;   // Returning the data from the function 
    }

    // Defining the basic scraping function
    function scrape_between($data, $start, $end){
        $data = stristr($data, $start); // Stripping all data from before $start
        $data = substr($data, strlen($start));  // Stripping $start
        $stop = stripos($data, $end);   // Getting the position of the $end of the data to scrape
        $data = substr($data, 0, $stop);    // Stripping all data from after and including the $end of the data to scrape
        return $data;   // Returning the scraped data from the function
    }
?>

// code.php

<?php

//include functions
include_once("include/functions.php");

// Set URL
$url = "https://www.instituteforsupplymanagement.org/ismreport/mfgrob.cfm";
$source = curl($url);

// Collect dataset

$arr = array("PMI","New Orders");
    foreach ($arr as $value) {

       $data = scrape_between($source,"<strong>$$value","</tr>");

       print_r($data);

}


?>
9
  • 2
    What does "no working" mean? Maybe you wanted to call the function print_r() without the dollar sign? Commented Jun 12, 2016 at 0:46
  • If i use a fixed value for $value, it will output the right information. However, when i try using a variable within a variable, there is zero output. Commented Jun 12, 2016 at 0:47
  • 1
    There is no variable variable usage here. You need at least two dollar signs $$value but your loop uses a reference &$value Commented Jun 12, 2016 at 1:00
  • 1
    @user6332864 Show us your real and full code and what you are actually (trying) to do. Commented Jun 12, 2016 at 1:05
  • 1
    If you just want to pass the values then just use one dollar sign. Two dollar signs is something different. Commented Jun 12, 2016 at 1:14

1 Answer 1

0

within the string the second $ is being treated as a string, so you end up with strings like '$PMI'. why not just assign your variable variable to a temp variable:

$start = $$value;
$data = scrape_between($source,"<strong>$start","</tr>");

also I don't know how you plan to use "New Orders" as a variable name what with the space and all. maybe you're trying to use "PMI" and "New Orders" in the string, in which case just drop the extra $

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.