Send post data in file_get_contents

1

Good afternoon 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 I get a JSON with the token. Can I do this using file_get_contents? I tried with the code below, removed from the PHP documentation, and it did not 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 only giving false and displaying the error: "file_get_contents (): php_network_getaddresses: getaddrinfo failed: Name or service not known in (...)"

Does anyone know how to solve it?

    
asked by anonymous 16.12.2015 / 17:22

1 answer

1

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

In some cases this problem also occurs 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 problem:

1 - Access the server via SSH 2 - Then type

#nano /etc/resolv.conf

Place Google IPs in the nameservers.

Example:

nameserver 8.8.8.8
nameserver 8.8.4.4

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

I took it from here: link

/ p>     
14.05.2017 / 00:49