2013-02-15 23 views
0

我的目标是让我的应用程序和 的订阅获得的所有数据标签爱Instagram的API实时挑战获取代码200,但无效数据

class IndexController extends App_Controller_Default 

{ 公共职能的indexAction() { $ data = $ this - > _ getSubscriptionTag(); Zend_Debug :: dump($ data); }

public function realtimecallbackAction() 
{ 
    $this->_helper->layout->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(true); 
    $allParams = $this->_getAllParams(); 
    $dataFile = ROOT_DIR.'/data.txt'; 
    file_put_contents($dataFile, implode(',',$_GET).implode(',',array_keys($_GET))); 
    if(isset($_GET['hub_challenge'])){ 
     $challenge = $_GET['hub_challenge']; 
     $file = ROOT_DIR.'/json.txt'; 
     $json = $this->_challenge($challenge); 
     file_put_contents($file, $json); 
    } 
    $request = $this->getRequest(); 
    if($request->isPost()){ 
     $filePostData = ROOT_DIR.'/postdata.txt'; 
     $postdata = file_get_contents("php://input"); 
     file_put_contents($filePostData, $postdata, FILE_APPEND | LOCK_EX); 
    } 
} 

protected function _getSubscriptionTag($verifyToken='') 
{ 
    $config = array(
     'adapter' => 'Zend_Http_Client_Adapter_Curl', 
     'curloptions' => array(CURLOPT_FOLLOWLOCATION => true), 
    ); 
    $uri = 'https://api.instagram.com/v1/subscriptions/'; 
    $request = $this->getRequest(); 
    $callbackUrl= $request->getScheme().'://'.$request->getHttpHost().$request->getBaseUrl().'/index/realtimecallback'; 
    $client = new Zend_Http_Client(); 
    $client->setUri($uri); 
    $client->setConfig($config); 
    $client->setMethod(Zend_Http_Client::POST); 
    $client->setParameterPost('client_id', 'my_id'); 
    $client->setParameterPost('client_secret', 'my_secret'); 
    $client->setParameterPost('object', 'tag'); 
    $client->setParameterPost('aspect', 'media'); 
    $client->setParameterPost('object_id', 'love'); 
    $client->setParameterPost('verify_token', $verifyToken); 
    $client->setParameterPost('callback_url', $callbackUrl); 
    $response = $client->request(); 
    $httpCode = $response->getStatus(); 
    $httpHeaders = $response->getHeaders(); 
    $httpBody = $response->getBody(); 
    $data = array(); 
    try{ 
     $data = Zend_Json_Decoder::decode($httpBody,Zend_Json::TYPE_ARRAY); 
    } 
    catch(Exception $e){ 
     $data['error_type'] = 'malformed json'; 
     $data['error_message'] = $e->getMessage(); 
    } 
    var_dump($client->getLastRequest()); 
    return $data; 
} 

protected function _challenge($challenge) 
{ 
    $config = array(
     'adapter' => 'Zend_Http_Client_Adapter_Curl', 
     'curloptions' => array(CURLOPT_FOLLOWLOCATION => true), 
    ); 
    $uri = 'https://api.instagram.com/v1/subscriptions/'; 
    $client = new Zend_Http_Client(); 
    $client->setUri($uri); 
    $client->setConfig($config); 
    $client->setParameterGet('challenge', $challenge); 
    $client->setParameterGet('client_id', 'my_id'); 
    $client->setParameterGet('client_secret', 'my_secret'); 
    $response = $client->request(); 
    $httpCode = $response->getStatus(); 
    $httpHeaders = $response->getHeaders(); 
    $httpBody = $response->getBody(); 
    return $httpBody; 

} 

}

在json.txt文件

我得到 { “元”:{ “代码”:200}, “数据”:[]} ,如果我做的请求 https://api.instagram.com/v1/subscriptions?client_secret=CLIENT-SECRET&client_id=CLIENT-ID 我得到同样的 我不知道该走哪条路:( 你能帮助我,请

我也试图与 $客户 - > setParameterGet(“verify_token”,$挑战); 但它不工作:(

回答

0

:)

public function realtimeAction() 
{ 
    $file = ROOT_DIR.'/json.txt'; 
    $data = $this->_getSubscriptionTag(); 
    $request = $this->getRequest(); 
    if($request->isPost()){ 
     $filePostData = ROOT_DIR.'/postdata.txt'; 
     $postdata = file_get_contents("php://input"); 
     file_put_contents($filePostData, $postdata, FILE_APPEND | LOCK_EX); 
    } 
    Zend_Debug::dump($data); 
} 

public function realtimecallbackAction() 
{ 
    $this->_helper->layout->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(true); 
    if(isset($_GET['hub_challenge'])){ 
     $challenge = $_GET['hub_challenge']; 
     echo $challenge; 
     exit; 
    } 

}

非常感谢 http://thegregthompson.com/instagram-real-time-api-php/

+1

能否请您解释您的解决方案? – Nativ 2014-12-10 17:25:16