2017-02-12 194 views
-1

我想将以下PHP代码转换为JavaScript。将PHP代码转换为JavaScript

<body> 
<?php 

$opts = array('http'=>array('method'=>"GET", 
    'header'=>"User-agent: CUSTOM_USER_AGENT\r\n")); 

$context = stream_context_create($opts); 

$file = file_get_contents("http://example.com/file1", false, $context); 
    ///open http://example.com/file1 with CUSTOM_USER_AGENT 

$pattern = "/^Location:\s*(.*)$/i"; 

$location_headers = preg_grep($pattern, $http_response_header); 
    //find url where this redirects to 

echo $location_headers 
?> 
</body> 
+1

欢迎来到SO。请访问[帮助],看看有什么和如何问。在这种情况下,请添加努力,至少预期的行为和缺少分号 – mplungjan

+0

尝试添加一些更多的描述您的问题..! – Seeker

+0

那就是所有我只是想将此PHP代码转换为JavaScript。 –

回答

0

,除非你是的node.js

除非CORS启用

JavaScript不能从另一个起源访问文件不能在JavaScript重写这个。

AJAX也无法更改用户代理:

JQuery Ajax Request: Change User-Agent

最后什么文件,你在看什么?它看起来像你正在寻找一个位置标题声明 - 这甚至不会在Ajax结果中可见