Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][cURL]Podążanie za przekierowaniem bez użycia FOLLOWLOCATION
Forum PHP.pl > Forum > Przedszkole
Milek7
Witam.
Chcę przerobić pewnien kod (nie jest on mój) używający cURLa, tak aby nie używał CURLOPT_FOLLOWLOCATION. W internecie znalazłem kilka funkcji które mają to zrobić, jednak przy użyciu ich kod pokazuje
{"status":{"http_code":0},"contents":null}
a poprawny wynik powinien być taki:
{"status":{"http_code":200},"contents":"HTTP\/1.0 200 OK\r\nServer: aris\r\nExpires: Tue, 17 Jul 2012 10:17:46 GMT\r\nLast-Modified: Tue, 17 Jul 2012 10:17:46 GMT\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nContent-type: text\/html; charset=UTF-8\r\nSet itd.......................
Postanowiłem więc nie korzystać z gotowych funkcji, tylko zrobić to samemu. Mój kod podąża za przekierowaniem, jednak to co pokazuje różni się od poprawnego wyniku: {"status":{"http_code":200},"contents":"<!DOCTYPE html>\n<html lang=\"pl\">\n<head>\n\t\t<meta charset=\"utf-8\" \/>\n\t<title>Wirtualna Polska - www.wp.pl<\/title>\n\t<meta name=\"Expires\" content=\"0\">\n\t<meta http-equiv=\"Pragma\" content=\"no-cache\">\n\t<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n\t<meta http-equiv=\"X-XRDS-Location\ itd.........................
Kawałek oryginalnego kodu:
  1. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  2. curl_setopt($ch, CURLOPT_HEADER, true);
  3. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  4. list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2);
  5. $status = curl_getinfo($ch);
  6. curl_close($ch);
Zastąpiłem go tym:
  1. curl_setopt($ch, CURLOPT_HEADER, true);
  2. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  3. $exec = curl_exec($ch);
  4. $info = curl_getinfo($ch);
  5. $redir_code = array(302, 301);
  6. if (in_array($info['http_code'], $redir_code)) {
  7. list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', $exec, 2);
  8. preg_match_all("&\bhttp://\S+&",$header, $matches);
  9. $url_parsea = preg_match($valid_url_regex, $matches[0][0]);
  10. if ($url_parsea) {
  11. curl_setopt($ch, CURLOPT_URL, $matches[0][0]);
  12. $exec = curl_exec($ch);
  13. }
  14. }
  15. list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', $exec, 2);
  16. $status = curl_getinfo($ch);
  17. curl_close($ch);
Co zrobić aby wyniki dwóch kodów były takie same?
Z góry dziękuję za odpowiedź.
Milek7
Okazało się, że pozostała część kodu przystosowana była do FOLLOWLOCATION. Musiałem zastąpić:
  1. $decoded_json = json_decode( $contents );
  2. $data['contents'] = $decoded_json ? $decoded_json : $contents ;
na
  1. if ($redirected) {
  2. $decoded_json = json_decode( $header . $contents );
  3. $data['contents'] = $decoded_json ? $decoded_json : $header . $contents ;
  4. } else {
  5. $decoded_json = json_decode( $contents );
  6. $data['contents'] = $decoded_json ? $decoded_json : $contents ;
  7. }
oraz zmienić ten kod:
  1. curl_setopt($ch, CURLOPT_HEADER, true);
  2. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  3. $exec = curl_exec($ch);
  4. $info = curl_getinfo($ch);
  5. $redir_code = array(302, 301);
  6. if (in_array($info['http_code'], $redir_code)) {
  7. list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', $exec, 2);
  8. preg_match_all("&\bhttp://\S+&",$header, $matches);
  9. $url_parsea = preg_match($valid_url_regex, $matches[0][0]);
  10. if ($url_parsea) {
  11. curl_setopt($ch, CURLOPT_URL, $matches[0][0]);
  12. $exec = curl_exec($ch);
  13. }
  14. }
  15. list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', $exec, 2);
  16. $status = curl_getinfo($ch);
  17. curl_close($ch);
na
  1. curl_setopt($ch, CURLOPT_HEADER, true);
  2. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  3. $exec = curl_exec($ch);
  4. $info = curl_getinfo($ch);
  5. $redir_code = array(302, 301);
  6. $redirected = 0;
  7. if (in_array($info['http_code'], $redir_code)) {
  8. $redirected = 1;
  9. list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', $exec, 2);
  10. preg_match_all("&\bhttp://\S+&",$header, $matches);
  11. $url_parsea = preg_match($valid_url_regex, $matches[0][0]);
  12. if ($url_parsea) {
  13. curl_setopt($ch, CURLOPT_URL, $matches[0][0]);
  14. $exec = curl_exec($ch);
  15. }
  16. }
  17. list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', $exec, 2);
  18. $status = curl_getinfo($ch);
  19. curl_close($ch);
i działa identycznie jak w oryginale.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.