如何从curl post响应中获取[redirect_url]的值?

时间:2015-09-13 12:03:14

标签: php curl http-post

我使用以下代码制作curl post。运行代码后,我看到一个包含大量信息的数组输出,包括重定向值。(print_r(curl_getinfo($curl_connection));)

[redirect_url] => http://somesite.org/browse.php?u=retwertwretetwe43545345=21&f=norefer

如何引用该重定向值(http://somesite.org/browse.php?u=retwertwretetwe43545345=21&f=norefer)并将其放在另一个变量中供以后使用?

<?php

//create array of data to be posted
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection =
  curl_init('http://www.somesite.com/target_url.php');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
                curl_error($curl_connection);

//close the connection
curl_close($curl_connection);

?>

修改 现在我获得了重定向网址的价值。我如何获取该URL的源代码进行解析?我当前的get_data没有带来任何东西!

$info = curl_getinfo($curl_connection); 
$url22 = $info['redirect_url'];
echo "redirectValue:".$url22;
$result2 = get_data($url22);
//close the connection
curl_close($curl_connection);



/* gets the data from a URL */

function get_data($url) {

  $ch = curl_init();

  $timeout = 5;

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

  $data = curl_exec($ch);

//perform our request
//$result = curl_exec($curl_connection);


//show information regarding the request
print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' .
                curl_error($ch);

  curl_close($ch);

  return $data;

}

0 个答案:

没有答案