2

I'm new to programming so maybe its something simple but when i run the script it gives the output that the return $response is not defined but it is defined in the array.

already tried moving the return statement but it doesn't help

this it just a piece of the code but this is the part that gives problems

function request($opt, $data) {
    $request = curl_init();

    foreach ($data as $status) {
        $data_string = json_encode($status);
        echo "Json: " . $data_string . PHP_EOL;

        $headers = array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)
        );

        $url = ENDPOINT . $opt['object'];

        curl_setopt($request, CURLOPT_URL, $url);
        curl_setopt($request, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($request, CURLOPT_POST, TRUE);
        curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($request, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);

        $response = curl_exec($request);
        if ($response == FALSE) {
            die('<p>Curl failed: ' . curl_error($request) . '</p>');
        }
    }

    return $response;
}
mitch
  • 45
  • 6
  • 1
    It is not defined if `$data` is empty / not iterable. – jeroen May 14 '19 at 07:45
  • 1
    Indeed, `$response` is *conditionally* defined. When its not defined, you will get *Undefined variable* errors – Qirel May 14 '19 at 07:46
  • 1
    Also note that you are overwriting your `$response` in the loop, so using a loop here does not seem to make much sense. – jeroen May 14 '19 at 07:49
  • There's no need to reassign `$url` each time through the loop. It's the same value every time, so just set it once before the loop. – Barmar May 14 '19 at 07:51

2 Answers2

2

1.You need to define $response = array(); before foreach() (Because if in any case $data is an empty array then $response is undefined. As it's defined inside foreach())

2.In your loop code you are over-writing $response variable. So modified that too like below:

$result = curl_exec($request);
if ($result == FALSE) {
   die('<p>Curl failed: ' . curl_error($request) . '</p>');
}
$response[] = $result;

Put common curl code outside of loop like this:-

function request($opt, $data) {

    $url = ENDPOINT . $opt['object'];

    $request = curl_init();curl_setopt($request, CURLOPT_URL, $url);

    curl_setopt($request, CURLOPT_POST, TRUE);
    curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($request, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);

    $response = array();

    foreach ($data as $status) {
        $data_string = json_encode($status);
        echo "Json: " . $data_string . PHP_EOL;

        $headers = array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)
        );

        curl_setopt($request, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($request, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($request);
        $response[] = $result;
        if ($result == FALSE) {
            die('<p>Curl failed: ' . curl_error($request) . '</p>');
        }
    }

    return $response;
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

You need to define $response before foreach() statement.

function request($opt, $data) {
$request = curl_init();
$responses = array();
foreach ($data as $status) {
    $data_string = json_encode($status);
    echo "Json: " . $data_string . PHP_EOL;

    $headers = array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string)
    );

    $url = ENDPOINT . $opt['object'];

    curl_setopt($request, CURLOPT_URL, $url);
    curl_setopt($request, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($request, CURLOPT_POST, TRUE);
    curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($request, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);

    $response =  curl_exec($request);
    $responses[] = $response;

    if ($response == FALSE) {
        die('<p>Curl failed: ' . curl_error($request) . '</p>');
    }
}

return $responses;
}

Why? Because If you want to access any variable outside the foreach() loop you have to define it first as a global variable.

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
Rahul raj
  • 11
  • 1