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'];
Related
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>';
}
}
I want to get links from a rss url . This is my code :
$doc = new DOMDocument();
$doc->load("http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml");
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$title = $node->getElementsByTagName('title')->item(0)->nodeValue;
$title=strip_tags($title);
$link=$node->getElementsByTagName('link')->item(0)->nodeValue;
}
I've used this code for several others URLs and all of them worked but on this one I get:
Warning:
DOMDocument::load(http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml): failed to open stream: HTTP request failed!
HTTP/1.1 403 Forbidden in /home/xxxxxxx/domains/xxxxxxx/public_html/data.php on line 14
Warning:
DOMDocument::load(): I/O warning: failed to load external entity "http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml"
in /home/xxxxxxx/domains/xxxxxxx/public_html/data.php on line 14
http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml
Line 14 is:
$doc->load("http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml");
Could you help me? Why does this request give me an error?
Thanks
Using the code above failed for me and it was not due to the comma as I commented. I found that, using curl, I was able to retrieve the xml file.
$c=curl_init('http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml');
curl_setopt( $c, CURLOPT_USERAGENT,'nginx-curl-blahblahblah' );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
$r=curl_exec( $c );
curl_close( $c );
$doc = new DOMDocument();
$doc->loadxml($r);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$title=$node->getElementsByTagName('title')->item(0)->nodeValue;
$title=strip_tags($title);
$link=$node->getElementsByTagName('link')->item(0)->nodeValue;
}
Add this code before calling your feed, this will change user agent.
$opts = array(
'http' => array(
'user_agent' => 'PHP libxml agent',
)
);
$context = stream_context_create($opts);
libxml_set_streams_context($context);
I am trying to use AlchemyAPI's php sdk. I am running the same example given on their Github page.
But when I try to run the example, I get this error message-
Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp\www\twitter-analysis\alchemyapi.php on line 261
What could be the reason? In alchemyapi.php, this is where the warning is occuring-
private function analyze($url, $params) {
//Insert the base URL
$url = $this->_BASE_URL . $url;
//Add the API Key and set the output mode to JSON
$url = $url . '?apikey=' . $this->_api_key . '&outputMode=json';
//Add the remaining parameters
foreach($params as $key => $value) {
$url = $url . '&' . $key . '=' . $value;
}
//Create the HTTP header
$header = array('http' => array('method' => 'POST', 'Content-type'=> 'application/x-www-form-urlencoded'));
//Fire off the HTTP Request
try {
$fp = #fopen($url, 'rb',false, stream_context_create($header));
$response = #stream_get_contents($fp);
fclose($fp);
return json_decode($response, true);
} catch (Exception $e) {
return array('status'=>'ERROR', 'statusInfo'=>'Network error');
}
}
}
fopen() returns the resource if it succeeds, and false if it didn't work. The most likely case for the fopen to not work is if the path is invalid or if you don't have access to the resource.
A similar question was asked here: https://stackoverflow.com/questions/18636680/php-warning-fclose-expects-parameter-1-to-be-resource-boolean-given
Try to test on live server.
My experience it gives error in local server where works in live server.
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);
}
So i am trying to parse data from an XML url and insert it into a table using php, which can be seen here, (please keep in mind there are more products than displayed on this page, i am not trying to get it for just this Product,the code below shows how i am parsing all products) but i keep getting the following errors:
[EDITED]
class DataGrabber {
//The URL where data will be extracted from, which is an XML file
protected $URL = "http://json.zandparts.com/api/category/GetCategories/44/EUR/";
public function call_api($data) {
if(count($data) == 0) return array();
$jsondata = array();
foreach($data as $entry){
$url = $this->URL . $entry['model'] . "/" . urlencode($entry['family']) . "/" . urlencode($entry['cat']) . "/" . $entry['man'] . "/null";
$json = file_get_contents($url);
$data = json_decode($json, true);
if(!empty($data['Products'])){
foreach ($data['Products'] as $id => $product) {
$jsonentry = array(
'productnumber' => $id,
'partnumber' => $product['strPartNumber'],
'description' => $product['strDescription'],
'manu' => $product['Brand']
);
$jsondata[] = $jsonentry;
}
}
}
return $jsondata;
}
}
[NEW ERRORS]
So i have fixed the error:
PHP Warning: file_get_contents(http://json.zandparts.com/api/category/GetCategories/44/EUR/ET10B/E Series/AC Adapter/Asus/null): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
in /home/svn/dev.comp/Asus.php on line 82
by using urlencode as shown in my code above
This warning below isnt finding the values for the url:
PHP Warning: file_get_contents(http://json.zandparts.com/api/category/GetCategories/44/EUR///04G265003580/Asus/null): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
as you can see after, 44/EUR there are three forward slashes with no data?? How would i resolve this??
The remote server appears to use the Accept HTTP header to choose the output format. With PHP default options it sends back JSON instead of XML:
<?php
echo file_get_contents('http://json.zandparts.com/api/category/GetCategories/44/EUR/ET10B/E%20Series/AC%20Adapter/Asus/null');
... prints:
{"Categories":[{"Brand":null,"Fami...
To specify an Accept header you need to retrieve the data with some other function, e.g.:
<?php
$context = stream_context_create(
array(
'http' => array(
'header' => "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n",
)
)
);
echo file_get_contents('http://json.zandparts.com/api/category/GetCategories/44/EUR/ET10B/E%20Series/AC%20Adapter/Asus/null', false, $context);
... prints:
<?xml version="1.0" encoding="utf-8"?><ProductCategory ...
Tweak it to your exact needs, I just copied the header from my browser.
Here's a code snippet that shows you how to get the values for strPartNumber, strDescription and Brand for all the products in that JSON data:
<?php
$url = 'http://json.zandparts.com/api/category/GetCategories/44/EUR/ET10B/E%20Series/AC%20Adapter/Asus/null';
$json = file_get_contents($url);
// Decode the JSON data as a PHP array
$data = json_decode($json, true);
if (!empty($data['Products'])) {
foreach ($data['Products'] as $id => $product) {
echo "Product #{$id}\n";
echo "Part number: {$product['strPartNumber']}\n";
echo "Description: {$product['strDescription']}\n";
echo "Brand: {$product['Brand']}\n\n";
}
}
Output:
Product #0
Part number: 04G265003580
Description: POWER ADAPTER 65W19V 3PIN
Brand: Asus
Product #1
Part number: 14G110008340
Description: POWER CORD 3P L:80CM,TW(B)
Brand: Asus