0

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

  • Please read [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/questions/285551/why-should-i-ot-upload-images-of-code-data-errors) to see how you can improve your question. – Andy Preston Mar 03 '23 at 10:23
  • hmm, you are missing something, your `var_dump($stderr);` isn't being displayed. perhaps something is maxing out your terminal's max scrollback length? try adding ```var_dump($stderr); file_put_contents("stderr.txt",$stderr);``` then after running it, open stderr.txt and see what it contains – hanshenrik Mar 03 '23 at 11:27
  • i have only shown some part of the result as the result was toooo big to be shown – Daniyal Ahmed Mar 03 '23 at 11:37
  • @DaniyalAhmed then show the parts of $stderr BEFORE all the PuTTY stuff – hanshenrik Mar 03 '23 at 11:40
  • @DaniyalAhmed try `cat stderr.txt | nc termbin.com 9999` , what do you get? – hanshenrik Mar 03 '23 at 11:40

1 Answers1

-1

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

?

hanshenrik
  • 19,904
  • 4
  • 43
  • 89