0

How to get response after third party url redirection in PHP with curl?

function curlRequest($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_URL, $url); 
    $result = curl_exec($ch); 

    return $result; 
}
kalehmann
  • 4,821
  • 6
  • 26
  • 36
Ricky
  • 3
  • 5
  • `CURLOPT_FOLLOWLOCATION` set to true will instruct cURL to follow redirects – apokryfos May 08 '19 at 12:04
  • https://stackoverflow.com/questions/3519939/how-can-i-find-where-i-will-be-redirected-using-curl – Maksim Fedorov May 08 '19 at 12:04
  • Dear Maksim ,I have a unique URL which is redirect any other URL For Example :- i open http://localhost:8080/Code-Diffusion/analistic/XzQwWQRfA.php?p_id=ABCD2&ID=4564 and i dont know where this url(another Website,another server,another country) will redirect ,so how can i get ending url status – Ricky May 08 '19 at 12:11

1 Answers1

0

curl_test_redirect.php

<?php

function curlRequest($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    $result = curl_exec($ch); 

    return $result; 
}

echo curlRequest('http://localhost//myWork/testPHP/curl_first_hit.php');
?>

curl_first_hit.php

<?php
echo 'hello in first hit';
header('Location: http://localhost//myWork/testPHP/curl_second_hit.php');
?>

curl_second_hit.php

<?php
echo 'hello in second hit';
?>
kalehmann
  • 4,821
  • 6
  • 26
  • 36
  • Dear Raja vikraman,I have a unique URL which is redirect any other URL For Example :- i open localhost:8080/Code-Diffusion/analistic/… and i dont know where this url(another Website,another server,another country) will redirect ,so how can i get ending url status – Ricky May 08 '19 at 12:20