Asked  7 Months ago    Answers:  5   Viewed   26 times

I know that using cURL i can see the destination URL, pointing cURL to URL having CURLOPT_FOLLOWLOCATION = true.

Example :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.example1.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$result = curl_exec($ch);
$info = curl_getinfo($ch); //Some information on the fetch
curl_close($ch);

$info will have the url of the final destination which can be www.example2.com. I hope my above understanding is correct. Please let me know if not!.

My main question is, what all type of redirection cURL will be able to know? Apache redirect, javascript redirects, form submition redirects, meta-refresh redirects!?

update Thanks for your answeres @ceejayoz and @Josso. So is there a way by which I can follow all the redirect programatically through php?

 Answers

25

cURL will not follow JS or meta tag redirects.

Wednesday, March 31, 2021
 
TecHunter
answered 7 Months ago
88

You should only call curl_close() when you know you're done with that particular handle, or if switching from its current state to a new one (ie: changing a ton of options via curl_setopt() would be faster by going from a clean new handle than your current "dirty" one.

The cookiejar/file options are only strictly necessary for maintaining cookies between seperate curl handles/invokations. Each one's independent of the others, so the cookie files are the only way to share between them.

Wednesday, March 31, 2021
 
huhushow
answered 7 Months ago
23

You need to add the Curl libraries to the command line PHP.ini.

You can probably just copy the file C:wampbinapacheApache2.2.xbinphp.ini to c:wampbinphpphp5.3.10php.ini (adjust for the actual directories on your system).

Wednesday, March 31, 2021
 
Fanda
answered 7 Months ago
79

Assuming curl is installed and enabled on the server, you should be able to do something like:

<?php
$post_data = array(
    'name' => 'Your Name',
    'email' => 'user@example.com',
    'web' => 'http://www.example.com',
    'comment' => 'your comment',
)
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.example.com/path/to/comment/post/script");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

curl_exec ($ch);
curl_close ($ch); 
?>

Another alternative, and possibly a more flexible one would be to use Guzzle which will handle the http request and response for you.

As a side note, the script you posted is currently dangerously written, you are directly inserting user provided data into the database without escaping it (e.g. by using mysql_real_escape_string), ideally you should be using a prepared statement via PDO or similar

Saturday, May 29, 2021
 
Isky
answered 5 Months ago
63

Use the location header flag:

curl -L <URL>

Tuesday, June 15, 2021
 
Hexaholic
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :