File get content failed to open stream - Json response - php

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 ?

Related

RestServer in Codeigniter: 401 Unauthorized response when loading in view

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);

file_get_contents() fails on specific urls with: failed to open stream: HTTP request failed! HTTP/1.0 429

I am trying to use the Steam API but whenever I use a file_get_contents() with the Steam Community URL I get this error:
Warning: file_get_contents(http://steamcommunity.com/market/priceoverview/?currency=1&appid=578080&market_hash_name=Gas%20Mask): failed to open stream: HTTP request failed! HTTP/1.0 429
If I use file_get_contents() on Google.com, for example, it returns the page without an error. I have tried to use curl which just returns random characters. Here is the code I used:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://steamcommunity.com/market/priceoverview/?currency=1&appid=578080&market_hash_name=Gas%20Mask");
//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);
echo $output;
Result:
��+��O��%
My domain was temporarily banned from making requests to the URL. I tested the code on another server and it worked.

Just call a https URL with PHP (without waiting for response)

I already googled and searched for an answer, and found also one, but I still get an error...
I just want to start a cronjob by calling a certain URL but I get this failure message:
PHP Warning: fopen(https://my-url.de): failed to open stream: HTTP request failed! in /var/www/vhosts/httpdocs/cronjob/import.php on line 41
I tried this one:
ignore_user_abort(true);
set_time_limit(0);
But it is not working... The url triggers a cronjob but this one needs to long to response...
So what would you suggest to call a cronjob in PHP?
I must call the cronjob by URL and I do not need to get a response!
Greetings and Thank You!
you can use curl instead of fopen as follows
$url = 'https://my-url.de';
$ch = curl_init($url);
// this will prevent printing out the contents , unless you are make echo $content
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

Simple html dom not working for a table[class]

I want to use the contents of a website and include them in mine with "Simple Html DOM", unfortunately the code which normally works for another websites, fails in this specific case:
<?php
// Note you must download the php files from the link above
// and link to them on this line below.
include_once('simple_html_dom.php');
$html = file_get_html('http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
$elem = $html->find('table[class=Descrizione]', 0);
echo $elem;
?>
Warning:
file_get_contents(http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.1 500 Internal Server Error in
public_html/elin-custom-code/simple_html_dom.php on
line 75
Fatal error: Call to a member function find() on a non-object in
public_html/elin-custom-code/sklad-1.php on line 9
As a link for the reference:-
file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
Most hosting provides now block the furl_open parameter which allows you to use file_get_contents() to load data from an external url.
You can use CURL or a PHP client library like Guzzle.
To be make sure i just use CURL to check what is the exact problem with the help of below code:-
<?php
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
echo "<pre/>";print_r($query);
?>
Output:- http://prntscr.com/a91nl5
So it seems that for security reason these method calls(CURL and file_get_contents) are blocked on that site.
You can try this link answers also, may be you will get some useful output:-
PHP file_get_contents() returns "failed to open stream: HTTP request failed!"
Good Luck.

file_get_contents throws 400 Bad Request error PHP

I'm just using a file_get_contents() to get the latest tweets from a user like this:
$tweet = json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline/User.json'));
This works fine on my localhost but when I upload it to my server it throws this error:
Warning: file_get_contents(http://api.twitter.com/1/statuses/user_timeline/User.json) [function.file-get-contents]:failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request...
Not sure what might be causing it, maybe a php configuration I need to set on my server?
Thanks in advance!
You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling:
// make request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
curl_close($ch);
This may give you a better idea why you're receiving the error. A common error is hitting the rate limit on your server.
You can use file_get_contents adding the ignore_errors option set to true, in this way you will get the entire body of the response in case of error (HTTP/1.1 400, for example) and not only a simple false.
You can see an example here: https://stackoverflow.com/a/11479968/3926617
If you want access to response's headers, you can use $http_response_header after the request.
http://php.net/manual/en/reserved.variables.httpresponseheader.php
Just a little addendum on Ben's answer.
According to the PHP manual the CURLOPT_URL option may be set when inizializing the cURL handle with curl_init().
// make request
$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
curl_close($ch);

Categories