Simple html dom parser return error 500
Asked Answered
W

4

16

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 ??

Woad answered 6/12, 2015 at 9:31 Comment(1)
How do you mean can not find, If i type it manually it works?Woad
U
6

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:[email protected]
Unaware answered 8/12, 2015 at 12:26 Comment(0)
R
4

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);
Roundy answered 12/12, 2015 at 17:36 Comment(1)
stream_context_create(): options should have the form ["wrappername"]["optionname"] = $valueWoad
N
3

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

enter image description here

Nanosecond answered 12/12, 2015 at 5:30 Comment(2)
Missing parameter: amt.Woad
You can check amt parameter into href, it is there.Nanosecond
U
3

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>';
    }
}
Unreadable answered 13/12, 2015 at 8:56 Comment(2)
500 error could be internal error too, especially with Simple html Dom memory leak, or just a miss-config server, or could be anything...Carabiniere
@Carabiniere As the error comes from the remote server, you can pretty exclude a memory leak in this locally used library. The locally thrown error is perfectly explained by the error message "failed to open stream: HTTP request failed!". The subject of this question is more to explain this error and why does the remote server threw this error when we call this code inside a loop while the remote config does not change.Unreadable

© 2022 - 2024 — McMap. All rights reserved.