Search code examples
phparraysreturn

Return is undefined but it's defined


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;
}

Solution

  • 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;
    }