2013-02-15 69 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