I'm using the restserver in Codeigniter from Chris Kacerguis and I setup rest_auth with session.
When I'm logged in, I access the page whose view.php get the response through file_get_contents and it's returning 401 Unauthorized.
When I access the API's direct page in a new tab, it returns the json correctly.
the view.php is loading the API content in this way:
json_decode(file_get_contents('https://example.com/api/proces/'), true);
the response:
A PHP Error was encountered
Severity: Warning
Message: file_get_contents( I needed to remove the link ): failed to open >stream: HTTP request failed! HTTP/1.1 401 Unauthorized
What's happening?
I see your example url is using "https" and it is possible that your Apache / Nginx server is not configured correctly to handle the SSL in this way, but it would work fine with the certificate in the browser.
I would suggest you use curl as an alternative instead.
json_decode(curl_get_contents('https://example.com/api/process/'));
from here https://github.com/zg/curl_get_contents
Or of course you could use curl directly.
$url = 'https://example.com/api/process/';
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HEADER, false);
$data = curl_exec($request);
curl_close($request);
Related
I am doing a cURL request to fetch information using api but when I use json_decode, it's not giving any information rather it's returning NULL value. Please go through these lines-
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,"http://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.html");
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
Is this correct way to make cURL request and fetch information using API, or suggest me how to do as I am new to this topic.
Your following line of code
$result=curl_exec($ch);
returning 301 Moved Permanently
since your URL is use HTTP only
http://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.htm
but this site runs on HTTPS, and server is setup to force/redirect this HTTP only URL to HTTPS i.e.
https://mkp.gem.gov.in/oem-cartridge/samsung-111s-toner-rst/p-5116877-68482402616-cat.html
You can either change your url to https or set follow redirection true using
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirect if any
but still it renders HTML not JSON.
But in your comment you says this same URL is working with node, in such case please cross check your URL or try to make same request using POSTMAN and see what is shows
For now i'm testing using file_get_content(url), but it's returning an error.
The request and response are in the same server:
warning: file_get_contents(https://192.168.1.15/adverts/locations): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in xxxxx
The Web-service returns a json object, so the function file_get_contents will receive a json.
I want to use cURL approach, but it doesn't work too.
So i thought to start ,fix this error first and get contents, and after try to debug why cURL is not working, since i'm getting content with this function
{"status":"success","alerts":[],"content":{"types":[{"id":"P","description":"Permanent"},{"id":"C","description":"Contract"}]}}
Try this with your webservice url and tell us if it throws some kind of Exception :
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "192.168.1.15/adverts/locations");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
Are you sure you can use HTTPS with your server ? Have you the right settings and certificates ?
I written simple php code to get some url content but it dosn't work
it return this error
file_get_contents(http://www.nature.com/nature/journal/v508/n7496/full/nature13001.html) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
here is my code. please help me.tnx
$content=file_get_contents('http://www.nature.com/nature/journal/v508/n7496/full/nature13001.html');
echo $content;
Here's an alternative to file_get_contents using cURL:
$url = 'http://www.example.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
You might want to add this curl_setopt($curl, CURLOPT_ENCODING ,""); if you encounter encoding problem.
Open the page using your browser and with the console open, see that the server does indeed send a 401 even when page is sent and viewable
On php, open the url in an alternate way to ignore the error (see http://php.net/manual/en/context.http.php)
You'll also notice that it's gzip-encoded, see http://php.net/manual/en/function.gzinflate.php
Happy hacking!
I wanted to handle 403 error without using server side error redirection methods(i.e. without using .htaccess file). Sometimes server returns a 403 error message (forbidden access). So is it possible to have PHP script which handles this 403 error message?
For example, Before showing error page, I would like to obtain server status when it my specific php page runs, and without making a redirection, I would just like to display custom message in the that page.
Some solutions for you.
Check for URL errors and make sure the actual web page is specified. Its common reason for a web site to return the 403 Forbidden error, when the URL is pointing to a directory instead of a web page. Which can be done using HttpRequest Class in PHP. You can use http_get to perform GET request. You can also Test URL here.
<?php
$response = http_get("URL", array("timeout"=>1), $info);
print_r($info);
?>
Output:
array (
'effective_url' => 'URL',
'response_code' => 403,
.
and so on
)
What is important for you is response_code with which you can play further.
Use of curl.
function http_response($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(!$head)
{
return FALSE;
}
return $httpCode;
}
$errorcode = http_response("URL"); //if success 200 otherwise different
How to display content of other page which has authentication enabled?
Warning: file_get_contents(http://abc/report.cmd?report=123): failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in //html/tracker/index2.php on line 13
$file_path = 'http://abc/report.cmd?report=123';
$content=file_get_contents($file_path);
echo $content;
Use the curl library to authenticate
<?php
// create a new cURL resource
$curl = curl_init();
// set URL and other appropriate options
curl_setopt($curl, CURLOPT_URL, "http://www.example.com/");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
// grab URL and pass it to the browser
curl_exec($curl);
// close cURL resource, and free up system resources
curl_close($curl);
?>
Authenticate first.., then request the request the content.
Here's an example of authenticating against HTTP Basic auth w/ PHP:
http://php.net/manual/en/features.http-auth.php