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.
– rray
@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"
– Luan Gabriel
Use a library called
GuzzleHttp
. It’s easier than doing thisfile_get_contents
;)– Wallace Maxters
@Wallacemaxters great recommendation!
– rray
@rray you know too. Nice ;)
– Wallace Maxters
use the Curl... http://php.net/curl
– Daniel Omine
@Danielomine tried using Curl but the result is still "false"
– Luan Gabriel
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
– Daniel Omine
@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
– Luan Gabriel
This makes no sense.. which error presented in Curl?
– Daniel Omine
only result = false
– Luan Gabriel