-1

For while I'm using file_get_contents, but idk why am I getting always error. i heard that cURL will not get that error.

Here is my code that i want to convert to cURL

<?php
$details1=json_decode(file_get_contents("http://2strok.com/download/download.json"));
 $details2=json_decode(file_get_contents($details1->data));
 header("Location: ".$details2->data); ?>

Nothing is wrong with this code on localhost, but when I do it on my web server (hosted by one.com) it doesn't work. It shows this error:

Warning: file_get_contents(url): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

Zabi
  • 23
  • 9
  • 1
    `file_get_contents()` may not fetch remote resources depending on config. [See this answer](https://stackoverflow.com/questions/10519238/how-can-i-load-a-remote-file-using-file-get-contents#answer-10519563). cURL is indeed probably the way forward. – Mitya Mar 01 '18 at 16:04
  • Idk where can i check php setting @Utkanos – Zabi Mar 01 '18 at 16:06
  • 1
    A 401 status means that the remote server is rejecting your request, usually because you have not provided the proper userid/password. However, in this case, it appears they may be blocking you based on your IP address. – Alex Howansky Mar 01 '18 at 16:06
  • @Utkanos That will generate a PHP warning, not a 401 response. `PHP Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0` – Alex Howansky Mar 01 '18 at 16:08
  • @AlexHowansky It's working very good sometimes in the same ip address I'm using, but otherday I'm getting error :D – Zabi Mar 01 '18 at 16:08
  • Set it up with cUrl. Here si a basic example. Take it from there, fill in your url and look at the result.. http://php.net/manual/en/curl.examples-basic.php – Daniel Mar 01 '18 at 16:09
  • Maybe you're causing a lot of traffic and they don't like you hitting their server. Try `curl http://2strok.com/download/download.json` from a shell and you'll probably get a 401 there as well. – Alex Howansky Mar 01 '18 at 16:10
  • 1
    cURL won't help you here. If the remote server is rejecting the request it wont care if the request is via cURL or via file_get_contents – apokryfos Mar 01 '18 at 16:10
  • Then i just don't know how to solve this problem. I made php.uni and allow_url_fopen = On; and .htaccess php_value allow_url_fopen On . getting 0 effect – Zabi Mar 01 '18 at 16:14
  • It's possible that you're hitting the URL too often and you're being rate limited. (Though that traditionally results in a 429 status.) – Alex Howansky Mar 01 '18 at 16:23
  • @IncredibleHat All right thanks for guiding ;) – Zabi Mar 01 '18 at 16:23
  • @AlexHowansky the orginal url is not http://2strok.com/download/download.json I have other url which is changing by php get all the time. – Zabi Mar 01 '18 at 16:24
  • That url I used it here is my own which is 100% similar to that other one. It's an youtube mp3 downloder. vnadigital.com – Zabi Mar 01 '18 at 17:13
  • @IncredibleHat If my ip is blocked then how the direct link is working good? – Zabi Mar 01 '18 at 17:30
  • Possible duplicate of [How to force file to download which is output of json file](https://stackoverflow.com/questions/48999780/how-to-force-file-to-download-which-is-output-of-json-file) – IncredibleHat Mar 02 '18 at 00:22

1 Answers1

0

You may still get a 401 with curl but you can try the following

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://2strok.com/download/download.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, json_decode($output)->data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

header("Location: ".json_decode($output)->data);

You can reuse the curl handle if you'd like

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://2strok.com/download/download.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, json_decode($output)->data);
$output = curl_exec($ch);
curl_close($ch);

header("Location: ".json_decode($output)->data)
joelrosenthal
  • 471
  • 4
  • 10
  • Thankyou for answering @joelrosenthal, but i'm getting this error this time:P . Warning: Cannot modify header information - headers already sent by (output started at /customers/b/2/f/just-smart.no/httpd.www/watch/index.php:2) in /customers/b/2/f/just-smart.no/httpd.www/watch/index.php on line 15 – Zabi Mar 01 '18 at 16:20
  • 1
    Dont output anything before the `header` function. This includes echos, prints, and vardumps debugging code. – IncredibleHat Mar 01 '18 at 16:22
  • @joelrosenthal Do you have any other idea? – Zabi Mar 01 '18 at 18:15
  • 1
    No, @IncredibleHat covered it well. Once you've sent content you cannot send headers. It may be worthwhile to read through [this](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php). – joelrosenthal Mar 01 '18 at 18:19