web services - PHP Socket Connection to an API -


i'm trying connect api using php sockets.

i've tried in many ways, can't working. know there other answers, no 1 works. can't achieve it. i'm trying @ least 2 days.

here error:

php_network_getaddresses: getaddrinfo failed: name or service not known

code (sockets - not work):

var $url = 'api.site.com.br/';  public function getusers(){      $path = "users/?accesstoken=c24f506fe265488435858925096bc4ad7ba0";     $packet= "get {$path} http/1.1\r\n";     $packet .= "host: {$url}\r\n";     $packet .= "connection: close\r\n";      if (!($socket = fsockopen($this->url,80,$err_no,$err_str))){       die( "\n connection failed. $err_no - $err_str \n");     }      fwrite($socket, $packet);     return stream_get_contents($socket); } 

code (curl - works!):

public function getusers2(){     $path = "users/?accesstoken=c24f506fe265488435858925096bc4ad7ba0";     $method = 'get';      $ch = curl_init();     curl_setopt($ch, curlopt_url, $this->url . $path);     curl_setopt($ch, curlopt_returntransfer, true);     curl_setopt($ch, curlopt_ssl_verifyhost, 0);     curl_setopt($ch, curlopt_ssl_verifypeer, false);     curl_setopt($ch, curlopt_customrequest, $method);     $output = curl_exec($ch);     $curl_error = curl_error($ch);     curl_close($ch);      return $output; } 

i can't give correct url, url working curl, should work sockets.

i solved doing following:

public function getusers(){     $path = "users/?accesstoken=c24f506fe265488435858925096bc4ad7ba0";     $fp = fsockopen($this->url, 80, $errno, $errstr, 30);     if (!$fp) {         echo "$errstr ($errno)<br />\n";     } else {         $out = "get /{$path} http/1.1\r\n";         $out .= "host: {$this->url}\r\n";         $out .= "connection: close\r\n\r\n";         fwrite($fp, $out);         while (!feof($fp)) {             echo fgets($fp, 128);         }         fclose($fp);     } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -