Send post data in file_get_contents

Asked

Viewed 603 times

1

Good afternoon to everyone, I have the following problem: I have to consume from a WS that returns me a Token, to use authentication in other WS. To get this token I have to send parameters via POST and receive a JSON with the token. Can I do this using file_get_contents? I tried with the code below, taken from the PHP documentation, and it didn’t work.

 $postdata = http_build_query(
    array(
        'var1' => 'some content',
        'var2' => 'doh'
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);

But my result is giving only "false" and presenting the error: "file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in(...)"

Someone knows how to solve?

  • I’ve used it, yes it’s pretty cool by the way.

  • @Can you tell me if the code is correct? my result is giving only false, and the error "file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in"

  • 2

    Use a library called GuzzleHttp. It’s easier than doing this file_get_contents;)

  • 1

    @Wallacemaxters great recommendation!

  • @rray you know too. Nice ;)

  • use the Curl... http://php.net/curl

  • @Danielomine tried using Curl but the result is still "false"

  • 1

    Are you sure the address is correct? Note that the error message reports a network error. You just can’t get information from the address. It can be 3 obvious things, 1- some problem or blocking on the network you are running scritpt on; 2- target lock; 3- invalid target

  • @Danielomine I’m sure. I managed to do the same thing, but with Javascript. And I use Postman to test the requests too, and it’s okay with the address

  • 1

    This makes no sense.. which error presented in Curl?

  • only result = false

Show 6 more comments

1 answer

1

The error "php_network_getaddresses: getaddrinfo failed: Name or service not known", usually happens when we try to use the stream_get_contents or file_get_contents function and pass a url in the parameter.

In some cases it also presents this same problem with email systems.

The truth is that in 90% of cases, the problem is in the configuration of the nameserver!

Follow the steps to fix this issue:

1 - Access the server via SSH 2 - After typing

#nano /etc/resolv.conf

Putting Google Ips in nameservers.

Example:

nameserver 8.8.8.8
nameserver 8.8.4.4

And say goodbye to "php_network_getaddresses: getaddrinfo failed: Name or service not known"

I took it from here: https://wp.me/p8Hv3H-1p

https://blog.hospedamais.com/servidor/resolvendo-o-erro-php_network_getaddresses-getaddrinfo-failed-name-or-service-not-known/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.