Curl response and redirect

Hi, I’m posting a form using curl:


//create the final string to be posted using implode()
$post_str = implode ('&', $post_items);
//Initialize cURL and connect to the remote URL 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://leads..html' );

//Instruct cURL to do a regular HTTP POST
curl_setopt($ch, CURLOPT_POST, TRUE);

//Specify the data which is to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);

//Tell curl_exec to return the response output as a string

//Follow 302 redirect
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  

// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

//verify https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

//Execute the cURL session
$response = curl_exec($ch );

//Close cURL session and file

echo $response;

$resp = explode("\
", $response);
$header = explode("\
", $resp[0]);

echo "<pre>";
echo "</pre>";


The response is:

success: https://www-somesite.html


[0] => success: https://www-somesite.html

My question is how to get the returned URL and redirect the user to it?

Probably easy but seems to be beyond my very limited php knowledge.


So you tell cURL to collect a URL, and it is doing that bit fine, you just want to know how to then redirect the user?

// do your curl stuff and end up with
$url = "";

// do some kind of error check to make sure $url is a URI, then
header( "Location: $url ") ;

The big gotcha with using header() is that your entire php script must output absolutely nothing prior to calling header(), not a white space nor line end, nothing. If you have error_reporting turned on and PHP throws even a non-critical warning then this is output to the browser and you will get a nasty error message instead of a redirect.

I thought it may be header.
My main issue tho is how to get the url from the response- success: https://www-somesite.html so i can put into variable?

Not exactly sure, but try:

$target = explode(": ", $response);

$url = $target[1];

else show us the full output of :

var_dump( $response );

Works a treat