php在发布请求后获取位置标题

时间:2015-07-26 18:33:41

标签: php

我需要获取以下请求的位置标头:

当我在chrome中执行它时,

This就是Chrome网络开发工具的请求。

here是Chrome网络开发工具中的重定向请求,以防万一由于某种原因需要它。

我尝试过以下事项:

这似乎只给我一个空洞的答案:

<?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);
?>

我也试过这个:

<?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);
?>

最后一个给出了以下结果:

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

如您所见,没有位置标题。

请注意我已经看过一些类似的帖子(比如问题6532753,我无法链接它,因为我已经处于我的2链接限制),但它们似乎对我不起作用。

如何从原始屏幕截图中标记的请求中检索位置标头?我错过了什么?

编辑:复制我的代码时我非常努力,对不起

1 个答案:

答案 0 :(得分:1)

我最终使用像@ frz3993建议的卷曲一样工作(并且在发送一个空白的'cookies'标题后,服务器似乎不喜欢它没有它)

这是我现在使用的代码:

<?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();
?>

请注意,即使我的OP的第二个建议给了我标题,它也没有给我curl现在做的位置标题。 (不要问我为什么:p)