Applications and ToolsBeginningDeveloperFeaturedNewbiePHPProgramming LanguageSocial Media MarketingSocial Network

ดึงข้อมูล Socialcam API ด้วยภาษา PHP ฉบับผู้เริ่มต้น

ขั้นตอนการเขียนโปรแกรมภาษา PHP ร่วมกับ API ของ Socialcam เพื่อดึงข้อมูลเบื้องต้นมาแสดงผลบนหน้าเว็บไซต์ของเรา ผ่าน Web Services แบบง่ายที่สุด และทำได้จริงไม่ต้องพูดพร่ำทำเพลงครับมาเริ่มกันเลยดีกว่า

เริ่มต้นไปที่เว็บไซต์ https://socialcam.com/developers/ เพื่อทำการสมัคร API ของ Developer ครับ

Screen Shot 2557-01-30 at 4.36.52 PM

กดที่ Create your App ครับ แล้วทำการสมัครสมาชิก ในตัวอย่างผมทดสอบในเครื่องของผม

Screen Shot 2557-01-30 at 4.37.23 PM

เชื่อมต่อ บัญชี Facebook ไปเลย คนเคยส่องกันอยู่แล้ว

Screen Shot 2557-01-30 at 4.40.00 PM

 

กรอกข้อมูลในตัวอย่างผมตั้งชื่อแอพผมว่า “WebHenCam” มาจาก Web Cam และ “เว็บ เห็น แคม” กด ตกลง

Screen Shot 2557-01-30 at 4.40.29 PM

ได้ API KEY และ APP ID, APP SECRET มาแล้ว เราก็เอามาใช้ครับ เปิดไฟล์ index.php ของเราขึ้นมา เขียนคำสั่งต่อไปนี้


เป็นการ เก็บค่าพื้นฐานของ แอพ เราครับ อย่าลืม เราต้องรู้นะครับว่า URL ของเรานั้นคืออะไร เช่นผม https://socialcam.com/v/oUd3SRHI ก็แบบว่า ID ของ Member ของผมคือ oUd3SRHI จะได้เอาใช้งาน

URL ในการเข้าระบบนั้น จะต้องเข้า 2 ขั้นครับ ขั้นแรกต้องเรียก Code มาก่อนให้เรียก URL ตามนี้

https://socialcam.com/oauth/authorize?app_id=YOUR_APP_ID&redirect_uri=REDIRECT_URI&response_type=code

ต่อมาเอา code มาอ้างเพื่อขอ Access Token  ครับเรียกตามนี้

https://socialcam.com/oauth/access_token?code=CODE&app_id=YOUR_APP_ID&app_secret=YOUR_APP_SECRET&grant_type=authorization_code

มาดู Code ของเราครับผมจะทำปุ่ม เข้าระบบ ตามนี้


Connect Social Cam

...

และเมื่อเข้าระบบจะมีการดึงข้อมูล Access token ตามนี้

access_token;
?>

การเรียก API ข้อมูลผู้ใช้งานคือ

https://api.socialcam.com/v1/users/self.json?access_token=USER_ACCESS_TOKEN

เขียนโปรแกรมอ่าน JSON ตามนี้

data->name;
	$picture=$users->data->picture_180;
	$link=$users->data->link;
}
?>

ลองเล่นดูจะเห็นการส่งค่า JSON ของเราคือ

{ "meta": 
       { 
        "code": 200 
        }, 
       "data": { "id": "1BQFQePr", "name": "The Roxie", 
                      "link": "http://socialcam.com/u/1BQFQePr", 
                      "picture": "http://u.scpic.net/1BQFQePr_50x50_5.png", 
                      "picture_180": "http://u.scpic.net/1BQFQePr_large_5.png", 
                      "bio": "Socialcam Rockstar", 
                      "counts": { "followers": 1604428, "following": 60, "public_videos": 136 } 
                    }
 }

Code ทั้งหมดที่เขียนจะเป็นตามนี้



Social Cam API



access_token;

//Profile
if($access_token){
	$get_member=file_get_contents('https://api.socialcam.com/v1/users/oUd3SRHI.json?access_token='.$access_token.'');
	$users=json_decode($get_member);
	$name=$users->data->name;
	$picture=$users->data->picture_180;
	$link=$users->data->link;
}


?>



Connect Social Cam


Your Profile: 

URL Profile:

ทดสอบ

Screen Shot 2557-01-30 at 6.06.10 PM

กด Connect Socialcam

Screen Shot 2557-01-30 at 4.42.59 PM

ให้ Authorize แอพพลิเคชันครับ

Screen Shot 2557-01-30 at 5.49.56 PM

ข้อมูลปรากฏแล้ว

เราจะดูค่าของคนอื่นก็ได้นะครับ แค่หา URL มาเช่นคนนี้

Screen Shot 2557-01-30 at 5.27.20 PM

หากลองยิง URL ตามตัวอย่าง

https://api.socialcam.com/v1/videos/bmrkPIwx.json?access_token=USER_ACCESS_TOKEN

จะได้ JSON ตามนี้ครับ

Screen Shot 2557-01-30 at 5.27.13 PM

ก็เอาเป็นว่า Socialcam API ก็ไม่ได้ยากเท่าไรครับ แต่ข้อเสียคือ ระยะเวลาของ Access Token ที่หมดเวล่เร็วไปนิดแค่นั้นครับ 😀

 

 

 

 

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน