K
K
KoiGva2014-07-08 11:30:30
PHP
KoiGva, 2014-07-08 11:30:30

How to get response content from server with 401 code?

Hello.
There is a certain REST service, when accessing which there is an option that the response will be json with the HTTP/1.0 401 Unauthorized code.
If you access it like this:
$str = @file_get_contents($url);
print_r($http_response_header);
echo $str;
, then I get an array with headers, json itself cannot be obtained.
The curl option also did not lead to positive results.
There is Json in the response for sure - when you go to $url in the browser, both it and 401 errors are returned.
Is there a way to get the same json from the response?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
@
@zipo, 2014-07-08
@koigva

There can be no magic. Both curl and file_get_contents return you all the content that the service gives.
Perhaps the difference in the answers comes from the difference in the request. For example, when you log in through a browser, cookies are additionally transmitted that can change the response of the service.
If you don’t want to show the service here, then try using browser plugins, for example, the same firebug for firefox and see exactly what is sent to the service. Then try to copy all this information and transfer it using curl or file_get_contents
Perhaps there is also a double authorization, one by means of a web service (standard http, a dialog appears with entering a login and password), the second is already more advanced using the server language. If you have logged in at least once in the browser, then all other requests it automatically transmits the necessary data and, for example, it reaches the second authorization, which returns json. And you, using curl and file_get_contents, may not even pass the first authorization, so nothing is returned to you at all.
In any case, you can see all this in firebug. You need to figure out what exactly your browser is sending to the server and then do the same in the code. Then the mysticism will disappear.

A
Alexander, 2014-07-08
@SashaSkot

$homepage = file_get_contents(' www.example.com ');
echo $homepage;
So what doesn't work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question