0

I want to send two curl requsts. First:

function auth($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0");
    curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $user_cookie_file);
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
        'Accept-Encoding: gzip, deflate, br',
        'Content-Type: application/x-www-form-urlencoded',
        'Connection: keep-alive',
        'Upgrade-Insecure-Requests: 1',
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $html = curl_exec($ch);
    curl_close($ch);

    $s01 = strpos($html, 'Redirecting', 0);

    if ($s01 === false) {
        echo "Not Found";
        $found_url = '';
    } else {
        echo "Found ";
        if (preg_match('/<a href="(.+)">/', $html, $matches));
        $found_url = $matches[1];

        return $found_url;
    }
}

And second:

function auth1($found_url) {
    $ch = curl_init($found_url);
    curl_setopt($ch, CURLOPT_URL, $found_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0");
    curl_setopt($ch, CURLOPT_COOKIEFILE, $user_cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $user_cookie_file);
    curl_setopt($ch, CURLOPT_POST, 0);

    $headers = array(
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3',
        'Accept-Encoding: gzip, deflate, br',
        'Content-Type: application/x-www-form-urlencoded',
        'Connection: keep-alive',
        'Upgrade-Insecure-Requests: 1',

    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $html = curl_exec($ch);
    echo $html;
    return $html;

}

I want to send two curl requsts. After sending first requst I need to get $found_url from first request and send second request with it. How to implement this correctly? I know this maybe a stupid question, but I stuck with it.

1
  • Code looks ok to me, just move the return on first method out of the else statement. Commented Jul 25, 2018 at 12:34

1 Answer 1

1

Modify the first auth to return always something by either adding a return to your 'not found part' to return maybe null or empty, or by moving the return to outside of your if statement. Then the following code should work.

$foundUrl = auth($url);
if($foundUrl){
   $html = auth1($foundUrl);
}
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.