Jump to content

how to use this function?


clankill3r

Recommended Posts

I switched from hosting and now i got lot's of errors with functions i used before.

And my hosting can't change shit due security...

 

Anyway, they gave me this link and said this could do what i need:

http://www.php.net/manual/en/function.curl-setopt.php#95027

 

<?php
function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) {
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($http_code == 301 || $http_code == 302) {
        list($header) = explode("\r\n\r\n", $data, 2);
        $matches = array();
        preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
        $url = trim(array_pop($matches));
        $url_parsed = parse_url($url);
        if (isset($url_parsed)) {
            curl_setopt($ch, CURLOPT_URL, $url);
            $redirects++;
            return curl_redirect_exec($ch, $redirects);
        }
    }
    if ($curlopt_header)
        return $data;
    else {
        list(,$body) = explode("\r\n\r\n", $data, 2);
        return $body;
    }
}
?>

 

I only have no idea of how to use it, i try this:

$url = "http://www.google.nl";
$redirects = 0;
$content = curl_redirect_exec($url, $redirects);

I get this error atm:

curl_setopt() expects parameter 1 to be resource, string given in

 

Before i used:

function getFileContents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, CURLREFERER);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

 

So must the $ch = curl_init(); be outside the function now? And if i must pass the curl_init(), then where do i pass the url?

Could someone give me a example of how to use it?

 

Link to comment
https://forums.phpfreaks.com/topic/249978-how-to-use-this-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.