Ascendant
Ascendant

Reputation: 827

In PHP, what is exactly a curl handle?

I've been looking for the answer but I could not get a clear explanation.

Is it an object? What attributes or methods does it have?

EDIT 1
So a handle is of the resource type and the resource type in PHP means some sort of external resource.

What then does this resource have under the hood ?

Upvotes: 3

Views: 9241

Answers (2)

T.Todua
T.Todua

Reputation: 56341

From PHP-8 CurlHandle has become object, instead of resource. To get some properties/manipulations onto that object, you need to use curl-functions.

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 157947

Under the hood a resource is a C pointer variable, that keeps alive unless you are explicitly closing it. Examples are open files, database connections or like in your case a curl handle.

If you dig more into C (PHP is written is C) you'll find that kind of handles very often.


To get the type of any object in PHP you can issue:

$type = gettype($variable);

If $type equals object you can get the class name using get_class():

if($type === 'object') {
    $type = get_class($variable);
}

If you try that with a curl handle, you will see it is a resource:

$curl = curl_init();
var_dump(gettype($curl)); // string(8) "resource"

Btw, on top of the documentation page of every PHP function you'll find the signature of that function, for curl_init() it looks like this:

resource curl_init ([ string $url = NULL ] )

You see, the return type is a resource. But however because of the loose typing system of PHP methods are allowed to return various return types. Especially in case of error most PHP methods will return false. Check the section Return Values for every method you'll use in PHP.

Upvotes: 8

Related Questions