skip 500 responses from looping file_get_contents - php

Here's the deal... I'm running a bunch of URL's through a foreach loop. Each URL will pass through fil_get_contents() but some will return 500 errors (expected), I want to skip/ignore any 500 errors, that are returned, but still pull the data for the valid responses. I have this setup, but still getting errors for undefined index on those 500 responses.
foreach($a['response']['groups']['users']['name'] as $key => $values)
{
$id = $values['uname']['id'];
$url = "http://thisurlimusing.com"."$id";
$context = stream_context_create(array('http' => array('ignore_errors' => true),));
$string = file_get_contents($url,false,$context);
$array = json_decode($string, true);
print_r($array['specific']);
}

your trouble is caused by the ignore_errors (http) set to true. In that case file_get_contents returns the error response text.
The following lines will work:
$string = file_get_contents($url,false);
if ($string !== FALSE) {
$array = json_decode($string, true);
}

Related

file_get_contents change "&" to "&amp"

I want get json from a url with file_get_contents but it replace & with &amp and it dosnt work this way
i tried curl and happened again
i try url directly too and it worked ,so url haven't problem
$url="https://api.kavenegar.com/v1/asdkljsadlkjsd/sms/send.json?receptor=09148523669&sender=100005252&message=testing";
$json = file_get_contents($url);
echo "1";
var_dump($json);
and this is the result:
( ! ) Warning:
file_get_contents(https://api.kavenegar.com/v1/asdkljsadlkjsd/sms/send.json?receptor=09148523669&sender=100005252&message=testing):
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
in C:\wamp64\www\te\func\testjson.php on line 22
Of course stackoverflow remove &amp from result url.
The trouble seems to be that it's returning an error status code. If so then see:
Ignoring errors in file_get_contents HTTP wrapper?
$url="https://api.kavenegar.com/v1/asdkljsadlkjsd/sms/send.json?receptor=09148523669&sender=100005252&message=testing";
$context = stream_context_create(array('http' => array('ignore_errors' => true),));
$json = file_get_contents($url, false, $context);
echo $json;
this is the answer :
$url = 'https://example.url?';
// http_build_query builds the query from an array
$query_array = array (
'search' => $string,
'from' => $from,
'to' => $to,
'format' => 'json'
);
$query = http_build_query($query_array);
$result = file_get_contents($url . '&' . $query);

PHP and JSON for Reddit

want to grab from a specific Reddit user some data.
There's a dynamic JSON file on the Reddit servers which can be accessed remotely.
The JSON file path is: http://www.reddit.com/user/tiagoperes/about.json
(where you can replace “tiagoperes” in the URL with whatever user you were trying to look up) - thank you Tom Chapin
Problem: I get the error message
http error 500: reddiant.com/reddit.php
Error Log:
PHP Warning:
file_get_contents(https://www.reddit.com/user/tiagoperes/about.json):
failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden on
line 5
PHP Fatal error: Uncaught exception 'InvalidArgumentException'
with message 'Passed variable is not an array or object, using empty
array instead' on line 8
Code:
<?php
$url = "https://www.reddit.com/user/tiagoperes/about.json";
$json = file_get_contents($url);
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:\n";
} else {
echo "$key => $val\n";
}
}
(inspired in this: http://codepad.org/Gtk8DqJE)
Solution: Ask for debug.
What's the problem right here?
Can not find a way to make it work and should be quite straightforward.
Thank you!
Was getting some troubles in the first procedure, so decided to change the approach.
Got it to work like that:
<?php
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: reddiant api script\r\n"
));
$context = stream_context_create($opts);
$url = "http://www.reddit.com/user/tiagoperes/about.json";
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
// Result:
var_dump($result);
//echo data
echo $result['data']['name'];

Simple html dom parser return error 500

I am using simple_html_dom.php library from this example
http://nimishprabhu.com/top-10-best-usage-examples-php-simple-html-dom-parser.html
But i got error 500 inside class, when I type url in browser it works ok?
I have some vaules in array like this
$result= Array (
[Avenya Group AG] =>
Array (
[link] => CHE-218.938.800
[href] => http://zh.powernet.ch/webservices/inet/HRG/HRG.asmx/getHRGHTML?chnr=0203038402&amt=020&toBeModified=0&validOnly=0&lang=1&sort=0 ) )
When I try something like this
foreach($result as $key => $value) {
$xmlFind = file_get_html($value['href']);
foreach($xmlFind->find('a') as $a) {
echo '<p>'.$a->href.'</p>';
}
}
I got error
A PHP Error was encountered
Severity: Warning
Message: file_get_contents(http://zh.powernet.ch/webservices/inet/HRG/HRG.asmx/getHRGHTML?chnr=0203038402&amt=020&toBeModified=0&validOnly=0&lang=1&sort=0): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
Filename: libraries/Simple_html_dom.php
Line Number: 76
But when I try manually like this
$xmlFind = file_get_html('http://zh.powernet.ch/webservices/inet/HRG/HRG.asmx/getHRGHTML?chnr=0203038402&amt=020&toBeModified=0&validOnly=0&lang=1&sort=0');
Result is there, also if i type that url i browser all is ok, only problem i have is when i try to loop an array ??
check http://php.net/manual/en/function.file-get-contents.php Notes section.
Please check your server settings for "fopen wrappers"
I tried the following
<?php
include('simple_html_dom.php');
$result= Array (
'Avenya Group AG' =>
Array (
'link' => 'CHE-218.938.800',
'href' => 'http://zh.powernet.ch/webservices/inet/HRG/HRG.asmx/getHRGHTML?chnr=0203038402&amt=020&toBeModified=0&validOnly=0&lang=1&sort=0' ) );
foreach($result as $key => $value) {
$xmlFind = file_get_html($value['href']);
foreach($xmlFind->find('a') as $a) {
echo '<p>'.$a->href.'</p>';
}
}
And got this
#
http://www.shab.ch/shabforms/servlet/Search?EID=7&DOCID=6890948
http://www.shab.ch/shabforms/servlet/Search?EID=7&DOCID=981331
http://zh.powernet.ch/webservices/inet/hrg/hrg.asmx/getExcerpt?Chnr=CH-020.3.038.402-5&Amt=20&Lang=1
mailto:info#powernet.ch
Proxy might be a problem. Use appropriate proxy.
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n",
'proxy' => 'tcp://221.176.14.72:80',
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('http://ifconfig.me/ip', false, $context);
var_dump($file);
Try this,
<?php
$result= Array (
'Avenya Group AG' =>
Array (
'link' => 'CHE-218.938.800',
'href' => 'http://zh.powernet.ch/webservices/inet/HRG/HRG.asmx/getHRGHTML?chnr=0203038402&amt=020&toBeModified=0&validOnly=0&lang=1&sort=0'
)
);
foreach($result as $arr_item){
if(is_array($arr_item)) {
if(isset($arr_item['href'])) {
echo file_get_contents($arr_item['href']);
}
}
}
?>
After executing above code, I got this response as shown into attached image.
If you still get an warning error, you can use curl to send get request.
so instead of echo file_get_contents($arr_item['href']); above
replace it with the following code.
$ch = curl_init($arr_item['href']);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
This message is returned by the remote server. It shows that the server might be unavailable at this time.
I think this might be caused by a too important amount of resources needed to execute the different requests you made within your loop. It can also be linked to some Denial of Service protection.
After reaching the maximum number of authorized connection, it returns "HTTP 500 Server Too Busy".
See: https://www.iis.net/configreference/system.webserver/asp/limits
The requestQueueMax attribute specifies the maximum number of concurrent ASP requests that are permitted into the queue. Any client browser that attempts to request ASP files when the queue is full is sent an HTTP 500 Server Too Busy error.
You can try to delay each of your call to the url with a sleep() if you're not time limited.
The best thing to do is contact the owner/sys admin of the remote web service in order to let him know about the problem so that he could investigate.
Depending of what you are doing in your script you can also ignore the error message and continue with the next call:
foreach($result as $key => $value) {
// added # to ignore the error
$xmlFind = #file_get_html($value['href']);
// continue to the next result
if (!$xmlFind) continue;
foreach($xmlFind->find('a') as $a) {
echo '<p>'.$a->href.'</p>';
}
}

Issue displaying json feed items using PHP

I am very new to json in combination with php and wonder why the following code is not working. My mission is displaying the title items one by one.
<?php
$json_string = file_get_contents("https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48");
$parsed_json = json_decode($json_string, true);
$parsed_json = $parsed_json['gigs'];
foreach($parsed_json as $key => $value)
{
echo $value['title'] . '<br>';
}
?>
The next issue will by, how would I be able to save the image item called cloud_img_med to my server into /grabbed
Your help would be greatly appreciated.
To answer your question about why this code isn't working:
I tried to set it up myself, and found out that the output fetched by file_get_contents is compressed. You can see that by reading the output in a simple var_dump:
var_dump($json_string);
To fix this, you'll need to use a custom context with no compression:
$context = stream_context_create(
array(
'http' => array(
'method' => "GET",
'header' => "Accept-Encoding: gzip;q=0, compress;q=0\r\n",
)
));
Then pass it as the third parameter:
$json_string = file_get_contents("https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48", false, $context);
I should do this JSON string readable
The code seems to work fine - I tried it on my local machine.
Running this
<?php
$str = <<<EOT
// JSON here...
EOT;
$parsed_json = json_decode($str, true);
$parsed_json = $parsed_json['gigs'];
foreach($parsed_json as $key => $value)
{
echo $value['title'] . '<br>';
}
?>
Gives me this output
replace the MGM lion with your pet or whoever<br>design creative Animal And Pet Company logo<br>`
Which is sort of what you want? If you're having trouble, maybe file_get_contents isn't allowed on your server and you can't get the JSON. Try hardcoding it into the $str variable like I did, and see if you get what you want.
With regards to saving the image, you can check this answer here: https://stackoverflow.com/a/724449/4396258
I think the problem might be the data you're getting from Fiverr's JSON API. At least in my test, I found it was using gzip encoding to compress the data you're requesting. So you have to uncompress it.
$json_string = file_get_contents("https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48");
$content = json_decode(gzinflate( substr($json_string,10,-8) ));
$gigs = $content->gigs;
foreach($gigs as $key => $value)
{
echo $value->title . '<br>';
}

JSON element returning NULL

I am new to JSON and having a problem with checking at getting the error message when there is an error. My code works fine when the result is not an error, so I do somewhat understand what I am doing.
This is the error JSON that I am trying to parse:
{
"error": {
"message": "Unsupported get request.",
"type": "GraphMethodException",
"code": 100
}
}
Here is my code that fails:
$jsonurl = "http://graph.facebook.com/JubilationDanceMinistry";
//valid $jsonurl = "http://graph.facebook.com/WhitworthACM";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
var_dump($json_output);
// This returns NULL
if (property_exists($json_output->error)) {
echo "<p>error: $json_output->error->{'message'} </p>";
} else {
echo "<p>no error :(</p>";
}
$facebook_id = $json_output->{'id'};
$facebook_name = $json_output->{'name'};
$facebook_link = $json_output->{'link'};
Because the url returns the 400 Bad Request.
By default, you can't use file_get_contents function to get the response content when the http status code is 400.
You need to set ignore_errors options to true.
$opts = array(
'http'=>array(
'ignore_errors' => true
)
);
$context = stream_context_create($opts);
$jsonurl = "http://graph.facebook.com/JubilationDanceMinistry";
$json = file_get_contents($jsonurl, false, $context);
var_dump($json);
You can't chain multiple ->'s with string interpolation.
You'll have resort to passing multiple arguments to echo or to string concatenation:
echo "<p>error: ", $json_output->error->{'message'}, " </p>";

Categories