L0laapk3
L0laapk3

Reputation: 958

php get location header after post request

I need to get the location header the following request:

This is what the request looks like from the chrome network dev tool when I execute it in chrome.

here is the redirect request in chrome network dev tool, just in case its needed for some reason.

I've tried the following things:

this seems to only give me an empty answer:

<?php
$url = $_GET["url"];
if (0 === preg_match('/form_build_id" value="form-([^"]*)"/', file_get_contents('http://anything2mp3.com/'), $matches)) exit("false");
$id = $matches[1];

$params = array('http' => array(
    'method' => 'POST',
    'content' => array(
        'url' => $url,
        'op' => "Convert",
        'form_build_id' => "form-" . $id,
        'form_id' => "videoconverter_convert_form"
    ),
    'max_redirects' => "0",
    'header' => "Content-Type:application/x-www-form-urlencoded"
));
stream_context_set_default($params);
$headers = get_headers("http://anything2mp3.com/?q=&mobile_detect_caching_notmobile&mobile_detect_caching_nottablet", false, $ctx);
echo implode("\n", $headers);
?>

i've also tried this:

<?php
$url = $_GET["url"];
if (0 === preg_match('/form_build_id" value="form-([^"]*)"/', file_get_contents('http://anything2mp3.com/'), $matches)) exit("false");
$id = $matches[1];

$params = array('http' => array(
    'method' => 'POST',
    'content' => array(
        'url' => $url,
        'op' => "Convert",
        'form_build_id' => "form-" . $id,
        'form_id' => "videoconverter_convert_form"
    ),
    'max_redirects' => "0",
    'header' => "Content-Type:application/x-www-form-urlencoded"
));
$ctx = stream_context_create($params);
$file = fopen("http://anything2mp3.com/?q=&mobile_detect_caching_notmobile&mobile_detect_caching_nottablet", 'rb', false, $ctx);
$headers = $http_response_header;
echo implode("\n", $headers);
?>

the last one gives me the following result:

HTTP/1.1 200 OK
Date: Sun, 26 Jul 2015 18:20:20 GMT
Content-Type: text/html; charset=utf-8
Connection: close
Set-Cookie: __cfduid=db7807d30a61600d31a18cc1a725150811437934820; expires=Mon, 25-Jul-16 18:20:20 GMT; path=/; domain=.anything2mp3.com; HttpOnly
Vary: Accept-Encoding
X-Cookie-Domain: .anything2mp3.com
Expires: Tue, 24 Jan 1984 08:00:00 GMT
Last-Modified: Sun, 26 Jul 2015 18:20:20 GMT
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
ETag: W/"1437934820"
Content-Language: en
X-Device: normal
X-GeoIP-Country-Code: US
X-GeoIP-Country-Name: United States
X-Speed-Cache-Key: /?q=&amp;mobile_detect_caching_notmobile&amp;mobile_detect_caching_nottablet
X-NoCache: Method
X-This-Proto: http
X-Server-Name: anything2mp3.com
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Server: cloudflare-nginx
CF-RAY: 20c21e3333eb23a8-IAD

as you can see, there is no location header.

note that I've seen some similar posts already (like question 6532753, I couldn't link it because I'm already at my 2 link limit), but they don't seem to work for me.

How do I retrieve the location header from request like marked in the original screenshot? What am I missing?

edit: I messed up pretty hard when copying my code, sorry for that

Upvotes: 2

Views: 2204

Answers (1)

L0laapk3
L0laapk3

Reputation: 958

I eventually got it to work using curl like @frz3993 suggested (and after sending in a blank 'cookies' header, the server didn't seem to like it without that)

here is the code that I use now:

<?php
$url = $_GET["url"];
if (0 === preg_match('/form_build_id" value="form-([^"]*)"/', file_get_contents('http://anything2mp3.com/'), $matches)) exit("false");
$longid = $matches[1];

$ch = curl_init("http://anything2mp3.com/?q=&mobile_detect_caching_notmobile&mobile_detect_caching_nottablet");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
        'url' => $url,
        'op' => "Convert",
        'form_build_id' => "form-" . $longid,
        'form_id' => "videoconverter_convert_form"
    )));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type:application/x-www-form-urlencoded",
    "Cookie:"
    ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$data = curl_exec($ch);
if (0 === preg_match('/Location: (.*id=(\d+))/', $data, $matches)) exit("false");
$redirect = $matches[1];
$id = $matches[2];
echo $redirect;
echo " - ";
echo $longid;
die();
?>

note that even tho the second suggestion from my OP gave me headers, it did not give me the location header which curl does now. (don't ask me why :p)

Upvotes: 1

Related Questions