-1

I am trying to make an api call with a curl wrapper. I made a new curl object and then tried to reference it and it says that i is undefined. This is the error below and it is refering to this line.

$curl->get("https://maps.googleapis.com/maps/api/geocode/json",

Notice: Undefined variable: curl in G:\wamp\www\voting\php\vote.php on line 18

<?php
    require ('/vendor/autoload.php');

    use \Curl\Curl;
    $key = "";

    $curl = new Curl();

    function getLat(){
        return explode(',',$_COOKIE['cord'])[0];
    }
    function getLong(){
        return explode(',',$_COOKIE['cord'])[1];
    }
    function getLocationInfo(){
        $lat = getLat();
        $long = getLong();
        $curl->get("https://maps.googleapis.com/maps/api/geocode/json",
            array(
                'latlng' => getLat().','.getlong(),
                'key' => $key,
            ));
        echo $curl->response->status;
    }
    function getDivision(){

    }


    ?>

<?php
    include("./php/vote.php");
    echo getLocationInfo();
    //echo $_COOKIE["cord"];
    ?>
Zach
  • 1
  • 3
  • You might want to read on function scopes – Damien Pirsy Mar 10 '16 at 16:35
  • In your getLocationInfo() function, it has no idea what $curl is (hence the undefined error), because you haven't actually defined it. The simplest ways to handle this is either define $curl immediately before the $curl->get() or create a function called getCurl that defines it, and then call that before your $curl->get(). – P. Gearman Mar 10 '16 at 16:36

3 Answers3

0

Obviously:

function getLocationInfo(){
    $lat = getLat();
    $long = getLong();
    $curl->get("https://maps.googleapis.com/maps/api/geocode/json",
      ^---where does this get defined inside the function?
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

You haven't passed $curl into your function so it is undefined. Please read up on Scopes inside methods!

This should fix it:

function getLocationInfo($curl){
        $lat = getLat();
        $long = getLong();
        $curl->get("https://maps.googleapis.com/maps/api/geocode/json",
            array(
                'latlng' => getLat().','.getlong(),
                'key' => $key,
            ));
        echo $curl->response->status;
    }

then when you call the function, add your parameter - $curl. ie: getLocationInfo($curl);

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
0

The problem is caused by variable scope. You have defined a variable called $curl which is out side of the scope of the functions in which you are trying to use.

You can fix it like this:

function getLocationInfo(){
    global $curl;
    $lat = getLat();
    $long = getLong();
    $curl->get("https://maps.googleapis.com/maps/api/geocode/json",
        array(
            'latlng' => getLat().','.getlong(),
            'key' => $key,
        ));
    echo $curl->response->status;
}

Another option is to pass the curl object as a parameter to the function like this:

function getLocationInfo($curl){
    $lat = getLat();
    $long = getLong();
    $curl->get("https://maps.googleapis.com/maps/api/geocode/json",
        array(
            'latlng' => getLat().','.getlong(),
            'key' => $key,
        ));
    echo $curl->response->status;
}

You should read up on http://php.net/manual/en/language.variables.scope.php

Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • Please, no globals. There are at least 2 better alternatives: 1) declare the variable inside the function, since apparently it's used only there; 2) pass it as a parameter to the function – Damien Pirsy Mar 10 '16 at 16:38