0

So I created a function with a cURL request inside and I get the data to print out when I do print_r which is great. I don't know how to array_map the data tho.

public function request()
{
    $resource = curl_init();
    curl_setopt(
        $resource,
        CURLOPT_URL,
        'https://etc....'
    );
    curl_setopt(
        $resource,
        CURLOPT_HTTPHEADER,
        ['API-Authorization: C:p']
    );
    curl_setopt(
        $resource,
        CURLOPT_REFERER,
        'http://' . $_SERVER['SERVER_NAME'] . '/'
    );
    curl_setopt(
        $resource,
        CURLOPT_USERAGENT,
        'F'
    );
    curl_setopt(
        $resource,
        CURLOPT_RETURNTRANSFER,
        1
    );
    $response = json_decode(curl_exec($resource));

    return $response;
}

So I have that cURL request and would like to array_map() it but I can't seem to get it to work properly, I have tried doing this:

    $response = json_decode(curl_exec($resource));
    $test[] = array_map('', $response);
    return $test;

With no luck, does anyone know what I might be doing wrong?

1
  • You are decoding a string, but need to encode an array. $response = json_encode(['html' => curl_exec($resource)]); Commented Oct 4, 2019 at 16:59

1 Answer 1

1

Add true parameter to json_decode to get array instead of object.

https://www.php.net/json_decode

$response = json_decode(curl_exec($resource), true);
return $response;
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.