-1

I want to convert the following PHP code into 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>
glennsl
  • 28,186
  • 12
  • 57
  • 75
  • 1
    Welcome to SO. Please visit the [help] to see what and how to ask. In this case please add effort, at least expected behaviour and the missing semicolon – mplungjan Feb 12 '17 at 06:54
  • Try adding some more description to your question..! – Seeker Feb 12 '17 at 06:54
  • Thats all i simply want to convert this php code to javascript. – Muhammad Ali Feb 12 '17 at 07:18
  • 5
    I'm voting to close this question as off-topic because it's asking us to write code from scratch. Please at least attempt the conversion for yourself, then ask for help with the parts that aren't working. – Matthew Strawbridge Feb 12 '17 at 10:36

1 Answers1

0

You cannot rewrite this in JavaScript unless you mean node.js

JavaScript cannot access files from another origin unless CORS is enabled.

Also AJAX cannot change the user agent:

JQuery Ajax Request: Change User-Agent

Lastly what file are you looking at? It looks like you are searching for a location header statement - that would not even be visible in an Ajax result

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236