Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.