2012-07-31 3 views
1

Foursquare API를 사용하고 있으며 JSON 양식 (https : // developer)으로 액세스 토큰을 받으려면 서버에 요청해야합니다. .foursquare.com/overview/auth). PHP를 사용하여 어떻게 할 수 있습니까?POST 요청을 보내고 PHP를 사용하여 JSON을 수신 + 해석하는 방법

온라인에서 최종 튜토리얼을 찾지 못했습니다. 그래서 여기에서 묻습니다. 나는 정말로 이해할 수없는 cURL과 관련된 몇 가지 물건을 보았습니다. 그래서 이것을 할 수있는 쉬운 방법이 있습니까? 내가 AJAX를 사용하기 전에 일을하고 매우 자명했지만, 그것의 모양 :(

누군가가 도와 드릴까요? 감사합니다

+0

특수 헤더를 보내야합니까? 아니면 간단한 게시물 요청입니까? – Tivie

+0

제가 말할 수있는 한, 단순한 게시물 요청 일 뿐이며 특별한 것은 없습니다. 감사합니다 :) – Taimur

답변

1

글쎄, 당신이 제공 한 링크를 따라,이 시도 : 리디렉션 링크

스크립트 (YOUR_REGISTERED_REDIRECT_URI)

if(isset($_GET['code'])) 
{ 
    $code = $_GET['code']; 
    $url = "https://foursquare.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=$code"; 

    $ch = curl_init($url); 

    curl_setopt($ch, CURLOPT_POST); 

    $json = curl_exec($ch); 

    var_dump($json); 
} 

참고 : 당신이 제공하는 튜토리얼을 읽은 후, 나는 'didn를 이 간단한 GET 요청의 경우 file_get_contents 아마 작동합니다,

$json = file_get_contents($url); 

을 t 요청 만 요청을 게시 할 수있는 참조를 참조하십시오, 그래서 당신이 (대신 컬) 시도 할 수 있습니다.

+0

답장을 보내 주셔서 감사합니다. 이것은 정확히 내가 찾던 일종의 것입니다. 곱슬 곱슬하고있는 것들을 정확히 안내해 주시겠습니까? 감사합니다 – Taimur

+0

$ json = file_get_contents ($ url); 아주 좋네요, 내 개인 키를 보내고 있기 때문에 POST가 필요하지 않습니까? – Taimur

+0

글쎄, 나는 그렇게 생각한다. 이 API는 GET 요청 만 필요로하는 것으로 보인다. PHP는 자체적으로주의를 기울인다. 제가 생각하기로는, CODE는 첫 번째 쉐이크 토큰으로 작동합니다. 응답은 유효한 json 또는 유사한 것이어야합니다. – Tivie

1
<?php 

# url_get_contents function by Andy Langton: http://andylangton.co.uk/ 

function url_get_contents($url,$useragent='cURL',$headers=false,$follow_redirects=false,$debug=false) { 

# initialise the CURL library 
$ch = curl_init(); 

# specify the URL to be retrieved 
curl_setopt($ch, CURLOPT_URL,$url); 

# we want to get the contents of the URL and store it in a variable 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 

# specify the useragent: this is a required courtesy to site owners 
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 

# ignore SSL errors 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

# return headers as requested 
if ($headers==true){ 
curl_setopt($ch, CURLOPT_HEADER,1); 
} 

# only return headers 
if ($headers=='headers only') { 
curl_setopt($ch, CURLOPT_NOBODY ,1); 
} 

# follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up 
if ($follow_redirects==true) { 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
} 

# if debugging, return an array with CURL's debug info and the URL contents 
if ($debug==true) { 
$result['contents']=curl_exec($ch); 
$result['info']=curl_getinfo($ch); 
} 

# otherwise just return the contents as a variable 
else $result=curl_exec($ch); 

# free resources 
curl_close($ch); 

# send back the data 
return $result; 
} 

?> 
으로 할 필요보다 훨씬 더, PHP에서 매우 복잡한 것 같다
관련 문제