2011-05-16 56 views
0

我正在使用一个外部API webservice,它返回一个json输出true或false。我访问像使用我的Zend项目中的webservice

http://site.com/api/valid 

一个URL,它给了我这样的事情,它看起来像JSON

"true" 

现在我手动访问URL,但我想现在就做编程方式从在我的zend项目中。我应该用什么来正确得到结果

回答

1

有很多方法。最简单的是使用file_get_contents()

$result = file_get_contents("http://site.com/api/valid"); 
// if result is truly json 
// data will be 
// array(0 => true) 
$data = json_decode($result); 

如果它是一个流行的web服务。可能会有一个为它编写的库。这是首选,因为它可以处理错误情况和角落情况。它首先在其周围。

1

听起来像你需要一种方法,可以抓住端点。嗯,有很多方法,但既然你已经使用Zend,你不妨阅读了关于http://framework.zend.com/manual/1.11/en/zend.http.client.adapters.html

这里有一个静态方法:

static function curl($url, $method, $params = array()){ 
      $client = new Zend_Http_Client($url); 
      if($method == "POST"){ 
       $client->setParameterPOST($params); 
      }else{ 
       $client->setParameterGet($params); 
      } 
      $response = $client->request($method); 
      return $response->getBody(); 
     } 

或者使用PHP的本地方法$响应=的file_get_contents($网址) ;

确保json_decode()您的回复。

相关问题