Daniyal Ahmed
Daniyal Ahmed

Reputation: 63

Public URL is accessible from curl, while it gives error using get_headers and file_get_contents

My PHP version is 7.4 and I'm trying to download twitter images using its API. I use get_headers to get mime type and other information but it doesn't work.

Whereas if I use curl it works. The error I get is

Name or service not known in php shell code on line 1

Using get_headers:

print_r(get_headers("https://pbs.twimg.com/media/FE9eUxLXEBAeyn3.jpg"));

PHP Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: Name or service not known in php shell code on line 1

PHP Warning: get_headers(https://pbs.twimg.com/media/FE9eUxLXEBAeyn3.jpg): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in php shell code on line 1

Using Curl:


curl_setopt($ch, CURLOPT_URL,"https://pbs.twimg.com/media/FE9eUxLXEBAeyn3.jpg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

curl_close($ch);
echo $server_output; 

Response

PuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPu

Server is a CentOS 7 server and internet has been allowed on it.

Allow_url_fopen is enabled:

php -i | grep "fopen"

allow_url_fopen => On => On

I have restarted httpd service still the error is there.

var_dump(gethostbynamel("pbs.twimg.com"));
bool(false);
var_dump(dns_get_record("pbs.twimg.com", DNS_ANY, $authoritative_name_servers, $additional_records, true)); 

PHP Warning: dns_get_record(): Numeric DNS record type must be between 1 and 65535, '268435456' given in php shell code on line 1
var_dump($authoritative_name_servers, $additional_records);

array(0) { } array(0) { }

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://pbs.twimg.com/media/FE9eUxLXEBAeyn3.jpg");
$stderrh=tmpfile();
curl_setopt_array($ch,array(CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$stderrh));
$server_output = curl_exec($ch);
/* https://bugs.php.net/bug.php?id=76268 */
rewind($stderrh); 
$stderr=stream_get_contents($stderrh);
fclose($stderrh);
var_dump($stderr);

PuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTY PuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTY PuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTY

Upvotes: 0

Views: 88

Answers (1)

hanshenrik
hanshenrik

Reputation: 21513

(not really an answer, but too many questionst to put it as comments..)

looks like a DNS issue to me, whatever DNS resolver your php/libcurl runtime is using, it isn't working. What do you get by

var_dump(gethostbynamel("pbs.twimg.com"));

? and what do you get from

var_dump(dns_get_record("pbs.twimg.com", DNS_ANY, $authoritative_name_servers, $additional_records, true));
var_dump($authoritative_name_servers, $additional_records);

? and what do you get from

$stderrh=tmpfile();
curl_setopt_array($ch,array(CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$stderrh));
$server_output = curl_exec($ch);
/* https://bugs.php.net/bug.php?id=76268 */
rewind($stderrh); 
$stderr=stream_get_contents($stderrh);
fclose($stderrh);
var_dump($stderr);

?

Upvotes: -1

Related Questions