curl로 받은 데이터로 html 형식으로 표현할때 아래에서

header('Content-Type: application/' . $content_type . '; charset=utf-8');

주석처리 해야한다.


아래코드는 

keyword로 검색한 후 결과값으로 이미지를 화면에 보여준다.

thumbnail_url 로 img src에 세팅하면....

속도가 아주~ 빠르다. 내가 검색한 데이타의 image가 이렇게 빠르게 보여지는게 놀랍다고 느껴진다.


json object 개수를 셀때 count를 이용해서 확인하고 

for문으로 image 를 그리고 있다. count($dt->documents)




<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title> image 검색하기 </title>
</head>
<body>

<?php
function request($path, $query, $content_type = 'json')
{
$api_server = 'https://dapi.kakao.com';
$headers = array('Authorization: KakaoAK {KAKAORESTFULAPIKEY}');
$opts = array(
CURLOPT_URL => $api_server . $path . '.' . $content_type . '?' . $query,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSLVERSION => 1,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => $headers
);

$curl_session = curl_init();
curl_setopt_array($curl_session, $opts);
$return_data = curl_exec($curl_session);

if (curl_errno($curl_session)) {
throw new Exception(curl_error($curl_session));
} else {
//print_r(curl_getinfo($curl_session));
curl_close($curl_session);
return $return_data;
}
}


$path = '/v2/search/image';
$content_type = 'json'; // json or xml
$params = http_build_query(array(
'page' => 1, //조회건수가 많으면 page수 변경하며 추가조회가능
'size' => 20, //한 페이지에 조회되는 데이터 건수
'query' => 'doitforyou.co.kr'
));

$res = request($path, $params, $content_type);

// header('Content-Type: application/' . $content_type . '; charset=utf-8');
$dt = json_decode($res); //json data type => php object로 변환
//json_encode : php object/array => json data type으로 변환

//var_dump($dt);

$tCount = $dt->meta->total_count;
$iCnt = count($dt->documents);
//echo "\r\n";
//echo "tCount:".$tCount;
//echo "icnt:". count($dt->documents);
for ($i=0; $i < $iCnt; $i++)
{
echo "<img src='".$dt->documents[$i]->thumbnail_url."'/>";
if((($i+1)%10 == 0 ) && ($i != 0)){
echo "<br>";
};
//echo "\r\n";
// echo $dt->documents[$i]->doc_url;
//echo "\r\n";
}
?>

</body>
</html>



'프로그래밍 > PHP' 카테고리의 다른 글

Open Api Request / Response Schema구성&실행  (0) 2018.08.19
curl kakao open api 도서검색  (0) 2018.08.10
curl kakao open api사용하기  (0) 2018.08.05
php CURL 사용하기  (0) 2018.07.31
Javascript로 Facebook로그인  (0) 2018.05.26

php curl을 이용하여 카카오 오픈api 검색엔진 사용하기


아래 예제는 다움포탈에서 doitforyou.co.kr로 검색했을경우 블로그 검색데이타(json type)를 가져와서

블로그title, 블로그url를 찍어보는 예제이다.


API는 아래 $path 에 따라서 검색되는 데이타가 달라진다.


$path = '/v2/search/blog';  --블로그

$path = '/v2/search/web';   --웹

$path = '/v2/local/search/address'; --주소


이 API를 이용해서 아이디어가 떠오른다면 APP또는 다른 서비스로 발전할 수 있을것 같다.


출처) https://devtalk.kakao.com .

 json 파싱부분 수정하더 정확한 url잊음

<?php
function request($path, $query, $content_type = 'json')
{
$api_server = 'https://dapi.kakao.com';
$headers = array('Authorization: KakaoAK {APIKEYAPIKEYAPIKEY} ');
$opts = array(
CURLOPT_URL => $api_server . $path . '.' . $content_type . '?' . $query,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSLVERSION => 1,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => $headers
);

$curl_session = curl_init();
curl_setopt_array($curl_session, $opts);
$return_data = curl_exec($curl_session);

if (curl_errno($curl_session)) {
throw new Exception(curl_error($curl_session));
} else {
curl_close($curl_session);
return $return_data;
}
}

$path = '/v2/search/blog';
$content_type = 'json'; // json or xml
$params = http_build_query(array(
'page' => 1,
'size' => 10,
'query' => 'doitforyou.co.kr'
));

$res = request($path, $params, $content_type);

header('Content-Type: application/' . $content_type . '; charset=utf-8');
echo $res;
//var_dump($res);
$dt = json_decode($res); //json data type => php object로 변환
//json_encode : php object/array => json data type으로 변환

$tCount = $dt->meta->total_count;
echo "\r\n";
echo "tCount:".$tCount;
echo "\r\n";

for ($i=0; $i < $tCount; $i++)
{
echo $dt->documents[$i]->title;
echo "\r\n";
echo $dt->documents[$i]->url;
echo "\r\n";
}
?>


'프로그래밍 > PHP' 카테고리의 다른 글

curl kakao open api 도서검색  (0) 2018.08.10
curl kakao open api 이미지검색하기  (0) 2018.08.06
php CURL 사용하기  (0) 2018.07.31
Javascript로 Facebook로그인  (0) 2018.05.26
php 패스워드 암호화 bcript  (0) 2018.05.02

Windows에서 php CURL 사용하기

오픈API를 사용하기 위해서 php에서 CURL이 동작하도록 설정한다..

 

Step1) C:\windows\php.ini 의 파일 수정

extension=php_curl.dll  사용하도록 수정

Step2) C:\APMorg\php 폴더의

libeay32.dll, ssleay32.dll  C:\Windows\System32 폴더로 복사

Step3) 윈도우 서비스의 아파치를 재시작 한다.

       아파치 환경에서 php를 구동하기 위해서 php.ini정보를 읽어 서비스 하기 때문에 재 기동 해야함.





phpinfo.php를 통해 반영된것 확인 가능하다.

curl

cURL supportenabled
cURL Information7.54.1


또는 아래 코드로도 확인가능하다.


<?php

if(extension_loaded("curl")){

echo "cUrl extension is loaded";

}else{

 echo "cUrl extension is not available";

}

?>


로컬 개발PC : 윈도우8 apm세팅

웹호스팅 중인 : doitforyou.co.kr 의 curl정보 확인. 

curl

cURL supportenabled
cURL Information7.29.0
Age3

 

'프로그래밍 > PHP' 카테고리의 다른 글

curl kakao open api 이미지검색하기  (0) 2018.08.06
curl kakao open api사용하기  (0) 2018.08.05
Javascript로 Facebook로그인  (0) 2018.05.26
php 패스워드 암호화 bcript  (0) 2018.05.02
unlink(=delete) 메소드 이용하기  (0) 2018.04.22

자바스크립트로 fb API연동하여 로그인하기



그래프 API 탐색기 이용하기

url : https://developers.facebook.com/tools-and-support 



appid 에서 가져올 권한 선택추가 선택


default : id, name 

추가로 가져올 정보 : email






위 api 탐색기로 선 테스트 후 

아래처럼 javascript로 코드작성.

참고url : https://developers.facebook.com/docs/facebook-login/web#logindialog



<<html code>>


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Page Title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!--
reference to : https://developers.facebook.com/tools-and-support
-->
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '2028791704043595',
     cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse social plugins on this page
version : 'v3.0' // use graph api version 2.8
});
    FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};

(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

   /*
     콜백에 제공되는 response 개체에는 다음과 같은 여러 필드가 포함되어 있습니다.
    {
        status: 'connected',
        authResponse: {
            accessToken: '...',
            expiresIn:'...',
            signedRequest:'...',
            userID:'...'
        }
    }코드 복사
    status는 앱 사용자의 로그인 상태를 지정합니다. 상태는 다음 중 하나일 수 있습니다.
    connected - 사용자가 Facebook에 로그인하고 앱에 로그인했습니다.
    not_authorized - 사용자가 Facebook에는 로그인했지만 앱에는 로그인하지 않았습니다.
    unknown - 사용자가 Facebook에 로그인하지 않았으므로 사용자가 앱에 로그인했거나 FB.logout()이 호출되었는지 알 수 없어,
Facebook에 연결할 수 없습니다.
    connected 상태인 경우 authResponse가 포함되며 다음과 같이 구성되어 있습니다.
    accessToken - 앱 사용자의 액세스 토큰이 포함되어 있습니다.
    expiresIn - 토큰이 만료되어 갱신해야 하는 UNIX 시간을 표시합니다.
    signedRequest - 앱 사용자에 대한 정보를 포함하는 서명된 매개변수입니다.
    userID - 앱 사용자의 ID입니다.
    앱에서 앱 사용자의 로그인 상태를 알게 되면 다음 중 하나를 수행할 수 있습니다.
    사용자가 Facebook과 앱에 로그인한 경우 앱의 로그인된 환경으로 리디렉션됩니다.
    사용자가 앱에 로그인하지 않았거나 Facebook에 로그인하지 않은 경우 FB.login()을 사용하여 로그인 대화 상자에 메시지를
표시하거나 로그인 버튼을 표시합니다.
        */
   function statusChangeCallback(response) {
    
      console.log('statusChangeCallback');

      if (response.status === 'connected') {
        console.log(response.authResponse.accessToken);
        // Logged into your app and Facebook.
        testAPI();
      } else if (response.status === 'not_authorized') {
        document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
      } else {
        document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.';
      }

  }

function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
  
function testAPI() {
  console.log('Welcome! Fetching your information.... ');
  FB.api(
    '/me',
    {"fields":"id,name,email"},
    function(response) {
       // Insert your code here
      console.log('Successful login for: ' + response.name);

      document.getElementById('status').innerHTML = JSON.stringify(response);
    }
  );
}

function fblogout(){

  FB.logout(function(response) {
    window.location.reload();
  });
}

</script>

<fb:login-button
scope="public_profile,email"
onlogin="checkLoginState();">
</fb:login-button>

<input type="button" onclick="javascript:fblogout();" value ="로그아웃" />
<div id="status"/>

</body>
</html>


로그인페이지>>


로그인후 fb의 개인정보 받아 display



로그인은 잘 되나, 로그아웃은 아래와 같은 에러내용으로 console log를 찍고 로그아웃이 안되고 있음. 

유투브 동영상등 찾아보고 진행했지만 해결못함.


Refused to display 'https://www.facebook.com/home.php' in a frame because it set 'X-Frame-Options' to 'deny'.


'프로그래밍 > PHP' 카테고리의 다른 글

curl kakao open api사용하기  (0) 2018.08.05
php CURL 사용하기  (0) 2018.07.31
php 패스워드 암호화 bcript  (0) 2018.05.02
unlink(=delete) 메소드 이용하기  (0) 2018.04.22
directory entry 읽어 파일리스트업  (0) 2018.04.20

스크린스크래핑

WebBrowser이용해서 website에 렌더링된 html의 특정값을 추출한다.

예를 들어 특정 사이트 모 카테고리에 있는 내용을 모두 가져와서 데이터화하고 그것을 분석하고 싶을 때 스크래핑을 사용하면 편리하다.

 

아래 code

 

특정 카테고리의의 글list를 통해 key, title을 추출하고

Key를 이용해 본문내용의 text를 가져와서 db화 한 내용이다.

글의 pattern등 분석자료로 사용할 목적으로 데이터가 필요하나 해당사이트에서 DB를공개하진 않으니 이렇게 스크랩핑해서 자료화해서 사용하는 것이다.

이렇게 하지 않으면 사이트의 모든 사이트 내용을 클릭~클릭~클릭~ 하겠지.

 

이렇게 프로그램하기 전에 사전에 해당 사이트를 분석하고 맞게 프로그래밍하면 된다.


step1) 카테고리의의 글list를 통해 key, title을 추출 : webSiteContentsList();

 => 추출된 data를 수작업으로 insert스크립트만들어 TMP_HELP 에 insert함.

step2) Key를 이용해 본문내용의 text를 가져와서 db화 한 내용 : webSiteContentsDetail(oBrowser);


<c# code>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;

namespace ScrappingWinForms
{
public partial class Form1 : Form
{

int iq = 0;
string contentsKey = "";

string sConnectionString = "Server=localhost;contentsKey=root;Pwd=???;Database=??????";
string sContentsUrl = "http://www.doitforyou.co.kr/sread.html?key="; //fake url임
string sListUrl = "http://www.doitforyou.co.kr/sub.html?&sec=cc&section=culture&page="; //fake url임

String Url = string.Empty;

public Form1()
{
InitializeComponent();

//WebBrowser로 사이트 로딩시 자바스크립트 바인딩안되어 에러나는 경우.
//자바스크립트 오류(dialogbox) 무시하고 진행가능하도록 제어함.
this.webBrowserViewer.ScriptErrorsSuppressed = true;
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void btnSearch_Click(object sender, EventArgs e)
{
// surftheWeb();
if (iq < 200)
{
selectContentsKeyfromWebsite();
}
}

#region surftheWeb :WebBrowser에 SitePage 브라우징한다.
private void surftheWeb()
{
Url = txtUrl.Text;
webBrowserViewer.Navigate(Url);
}

private void surftheWeb(String sUrl)
{
webBrowserViewer.Navigate(sUrl);
}

#endregion


#region Select / Update from the WebSite

private void selectContentsKeyfromWebsite()
{
MySqlConnection oConnection = new MySqlConnection(sConnectionString);
oConnection.Open();

try
{
MySqlCommand cmd = oConnection.CreateCommand();
cmd.CommandText = "SELECT HELP_TOPIC_ID FROM TMP_HELP WHERE HELP_TOPIC_ID >= 100 AND DESCRIPTION = '' LIMIT 1";
MySqlDataAdapter aDap = new MySqlDataAdapter(cmd);
DataSet dsData = new DataSet();
aDap.Fill(dsData);
if (contentsKey != dsData.Tables[0].Rows[0][0].ToString())
{
contentsKey = dsData.Tables[0].Rows[0][0].ToString();
String sUrl = String.Empty;
sUrl = sContentsUrl + contentsKey;
surftheWeb(sUrl);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (oConnection.State == ConnectionState.Open)
{
oConnection.Close();
}
}
}
/// <summary>
/// key값으로 contents내용을 업데이트 한다.
/// </summary>
/// <param name="sHelp_topic_id"></param>
/// <param name="sDescription"></param>
private void updateContentonIldaro(String sHelp_topic_id, String sDescription)
{
MySqlConnection oConnection = new MySqlConnection(sConnectionString);
oConnection.Open();

try
{
MySqlCommand cmd = oConnection.CreateCommand();
cmd.CommandText = "UPDATE tmp_help set description = @description where help_topic_id = @help_topic_id";
cmd.Parameters.AddWithValue("@help_topic_id", int.Parse(sHelp_topic_id));
cmd.Parameters.AddWithValue("@description", sDescription);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (oConnection.State == ConnectionState.Open)
{
oConnection.Close();

//key를 다시 조회한다.
btnSearch_Click(null, null);
}
}
}
#endregion

/// <summary>
/// WebBrowser's DocumentCompleted Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void webBrowserViewer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser oBrowser = sender as WebBrowser;
//txtSource.Text = StreamConvertEUCKRtoUTF8(oBrowser);

this.txtUrl.Text = oBrowser.Url.AbsoluteUri;

//webSiteContentsList();
webSiteContentsDetail(oBrowser);
}

private void webSiteContentsDetail(WebBrowser oBrowser)
{
iq++; //반복수행위한 카운팅
getWebSiteContentsDetail(oBrowser);
}

/// <summary>
/// 게시판 글list, Paging있음
/// </summary>
private void webSiteContentsList() {

iq++; //paging 숫자

getData();

if (iq < 21)
{
String sUrl = sListUrl + iq.ToString();
surftheWeb(sUrl);
}
}

/// <summary>
/// 1.WebBrowser로 rendering된 html을 TagName으로 HtmlElementCollection에 담는다.
/// 2.HtmlElementCollection => HtmlElement에 담긴 InnerHtml분석한다.
/// 3.HtmlElement[]에 추출하려는 대상을 담는다.
/// </summary>
/// <param name="wb"></param>
/// <param name="tagName"></param>
/// <returns></returns>
private HtmlElement[] GetElementsByTagName(WebBrowser wb, string tagName)
{
var l = new List<HtmlElement>();

var els = wb.Document.GetElementsByTagName(tagName); // all elems with tag
foreach (HtmlElement el in els)
{
if (el.InnerHtml == null)
{
continue;
}

if(el.InnerHtml.StartsWith("<A href=\"read.html"))
{
l.Add(el);
}
}

return l.ToArray();
}


/// <summary>
/// 1.WebBrowser로 rendering된 html을 TagName으로 HtmlElementCollection에 담는다.
/// 2.HtmlElementCollection => HtmlElement에 담긴 요소들중 className을 이용해서 추출하려는 대상을 뽑는다..
/// 3.HtmlElement[]에 추출하려는 대상을 담는다.
/// </summary>
/// <param name="wb"></param>
/// <param name="tagName"></param>
/// <param name="className"></param>
/// <returns></returns>
private HtmlElement[] GetElementsByTagNClassName(WebBrowser wb, string tagName, string className)
{
var l = new List<HtmlElement>();

var els = wb.Document.GetElementsByTagName(tagName); // all elems with tag
foreach (HtmlElement el in els)
{

if (el.GetAttribute("className") == className)
{
l.Add(el);
}
}

return l.ToArray();
}

/// <summary>
/// 글 상세 페이지에서 추출하고자 하는 Text만 가져온다.
/// </summary>
/// <param name="oBrowser"></param>
private void getWebSiteContentsDetail(WebBrowser oBrowser)
{
var arrCollection = GetElementsByTagNClassName(webBrowserViewer, "td", "contentsbody");
for (int i = 0; i < arrCollection.Length; i++)
{
//Console.WriteLine(arrCollection[i].InnerHtml);

String sText = String.Empty;
//sText = arrCollection[i].InnerHtml;
sText = arrCollection[i].OuterText;

if (sText == null)
{
//글 text가 없는 경우가 있음
//case1) 이미지로 대체한 경우 img html tag가 글 대신 있음
sText = arrCollection[i].InnerHtml;
}
if (contentsKey.Equals(oBrowser.Url.AbsoluteUri.Replace(sContentsUrl, "")))
{
updateContentonIldaro(contentsKey, sText);
}
}
}

private void getData() {

// getting day and night temperature
var arrCollection = GetElementsByTagName(webBrowserViewer, "dt");

DataTable dt = new DataTable();

dt.Columns.Add("contentsKey", typeof(int));
dt.Columns.Add("Title", typeof(string));

Console.WriteLine("-- " + iq.ToString() + " --------------");
txtSource.Text = txtSource.Text + "-- " + iq.ToString() + " --------------" + "\r\n";

for (int i = 0; i < arrCollection.Length; i++) {
//Console.WriteLine(arrCollection[i].InnerHtml);

String sText = String.Empty;
sText = arrCollection[i].InnerHtml;
sText = sText.Replace("<A href=\"sread.html?key=", "").Replace("</A>", "").Replace("\"", "");

String[] arrSplit = sText.Split('>');

dt.Rows.Add(arrSplit[0],arrSplit[1]);

Console.WriteLine(arrSplit[0] + ":" + arrSplit[1]);

txtSource.Text = txtSource.Text + arrSplit[0] + ":" + arrSplit[1] + "\r\n";

}

}

#endregion


reference to :  https://www.codeproject.com/Tips/858775/Csharp-Website-HTML-Content-Parsing-or-How-To-Get


'프로그래밍 > c#' 카테고리의 다른 글

c#에서 mariadb사용하기  (0) 2018.11.10
WebBrowser.DocumentText 한글깨짐  (0) 2018.05.07
using MySqlConnector  (0) 2018.05.07

WebBrowser 객체를 이용해서 web페이지를 볼 때 DocumentText 메소드로 렌더링된 html을 읽어올 때 default utf-8 encoding type이라 이상태에서는 euc-kr의 한글은 깨진다

이 경우 스트림리더를 사용해서 실제 페이지의 encoding type으로 읽어들이면 된다.

 

샘플) WebBrowser.DocumentText 로 소스를 봤을경우.

 

<html>

<head>

<title><��̴ϽƮ ���� �ϴ�></title>

<meta http-equiv="Content-Type" content="text/html; charset=euc-kr" />

 

 

StreamReader를 이용해 페이지의 encoding type으로 읽어들인경우

 

<html>

<head>

<title><오늘은 어린이날 입니다></title>

 

<<C# Code>>

 

/// <summary>

        /// WebBrowser's DocumentCompleted Event

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void webBrowserViewer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {

            // ((WebBrowser)sender).Document.Window.Error +=   new HtmlElementErrorEventHandler(Window_Error);

 

            WebBrowser oBrowser = sender as WebBrowser;

 

            txtSource.Text = StreamConvertEUCKRtoUTF8(oBrowser);

                       

        }

 

        /// <summary>

        /// Using WebBrowser web page

        /// 웹페이지 Encoding type EUC-KR 이면 WebBrowser.DocumentText html 렌더링된 text 볼때 한글이 깨진다.

        /// 아래처럼 스트림리더로 EUC-KR type으로 읽어온다.

        /// </summary>

        /// <param name="oBrowser"></param>

        /// <returns></returns>

        private String StreamConvertEUCKRtoUTF8(WebBrowser oBrowser)

        {

            String sResult = String.Empty;

 

            Stream oStream;

            StreamReader oStreamReader;

            oStream = oBrowser.DocumentStream;

            oStreamReader = new StreamReader(oStream, System.Text.Encoding.GetEncoding(oBrowser.Document.Encoding));

            oStream.Position = 0;

            sResult = oStreamReader.ReadToEnd();

 

            return sResult;

        }


'프로그래밍 > c#' 카테고리의 다른 글

c#에서 mariadb사용하기  (0) 2018.11.10
WebBrowser이용한 스크린스크래핑  (0) 2018.05.09
using MySqlConnector  (0) 2018.05.07


c#에서 Mariadb를 연결할때 MySqlConnector를 사용한다.

VS에서 NgGet을 이용해서 쉽게 검색하고 설치할 수 있다. 

*NgGet 이놈 참 좋다~


MySqlConnector는 MIT 라이센스 그냥 사용하면 된다.





connector설치한김에 테스트까지 해봐야지요



c#코드


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

using MySql.Data.MySqlClient;


namespace ScrappingWinForms

{

    public partial class Form1 : Form

    {

        string sConnectionString = "Server=localhost;Uid=root;Pwd=???????;Database=???";


        String Url = string.Empty;


        public Form1()

        {

            InitializeComponent();


            //WebBrowser로 사이트 로딩시 자바스크립트 바인딩안되어 에러나는 경우.

            //자바스크립트 오류(dialogbox) 무시하고 진행가능하도록 제어함. 

            this.webBrowserViewer.ScriptErrorsSuppressed = true;

            webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);

        }


        private void btnSearch_Click(object sender, EventArgs e)

        {

            surftheWeb();

        }

        

        private void surftheWeb()

        {

            Url = txtUrl.Text;

            webBrowserViewer.Navigate(Url);

        }


        /// <summary>

        /// WebBrowser's DocumentCompleted Event

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void webBrowserViewer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

        {

            // ((WebBrowser)sender).Document.Window.Error +=   new HtmlElementErrorEventHandler(Window_Error);


            WebBrowser oBrowser = sender as WebBrowser;


            txtSource.Text = StreamConvertEUCKRtoUTF8(oBrowser);

                        

        }


        /// <summary>

        /// Using WebBrowser web page 

        /// 웹페이지 Encoding type 이 EUC-KR 이면 WebBrowser.DocumentText로 html로 렌더링된 text를 볼때 한글이 깨진다.

        /// 아래처럼 스트림리더로 페이지 EUC-KR type을 읽어들인다.

        /// </summary>

        /// <param name="oBrowser"></param>

        /// <returns></returns>

        private String StreamConvertEUCKRtoUTF8(WebBrowser oBrowser)

        {

            String sResult = String.Empty;


            Stream oStream;

            StreamReader oStreamReader;

            oStream = oBrowser.DocumentStream;

            oStreamReader = new StreamReader(oStream, System.Text.Encoding.GetEncoding(oBrowser.Document.Encoding));

            oStream.Position = 0;

            sResult = oStreamReader.ReadToEnd();


            return sResult;

        }

         

        private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)

        {

            //this.Text = webBrowser1.DocumentTitle.ToString();


            this.groupBox1.Text = webBrowserViewer.DocumentTitle;

        }



        private void loadTestData() {

            MySqlConnection oConnection = new MySqlConnection(sConnectionString);

            oConnection.Open();


            try

            {

                MySqlCommand cmd = oConnection.CreateCommand();

                cmd.CommandText = "SELECT * FROM TMP_help order by help_topic_id desc";

                MySqlDataAdapter aDap = new MySqlDataAdapter(cmd);

                DataSet dsData = new DataSet();

                aDap.Fill(dsData);


                dgViewSample.DataSource = dsData.Tables[0].DefaultView;


            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally {

                if (oConnection.State == ConnectionState.Open)

                {

                    oConnection.Close();

                }

            }

        }

      


        private void btnForTest_Click(object sender, EventArgs e)

        {

            loadTestData();

        }

     

    }

}



'프로그래밍 > c#' 카테고리의 다른 글

c#에서 mariadb사용하기  (0) 2018.11.10
WebBrowser이용한 스크린스크래핑  (0) 2018.05.09
WebBrowser.DocumentText 한글깨짐  (0) 2018.05.07

password 암호화 알고리즘 bcrypt


php script : 

아래 옵셥의 cost 값은 해싱의 속도를 나타낸다.

default : 10이며 값이 커질수록 해싱의 속도가 느려진다. 이는 오토 크래킹으로 부터 보안성을 높여 줄 수 있다.


<?php

/*
if (defined("CRYPT_BLOWFISH") && CRYPT_BLOWFISH)
echo "CRYPT_BLOWFISH is enabled!";
else
echo "CRYPT_BLOWFISH is not available";

*/
#첫번째 매개변수 - $password - 해시할 값(비밀번호)
#두번째 매개변수 - PASSWORD_BCRYPT - 해시에 사용될 알고리즘(PASSWORD_DEFAULT, PASSWORD_BCRYPT, ...)

$password ="tester";
$options15 = [ 'cost' => 15 ] ;
$options12 = [ 'cost' => 12 ] ;

$hashedValue = password_hash($password, PASSWORD_BCRYPT);

$hashedValueWithO12 = password_hash($password, PASSWORD_BCRYPT, $options12);
$hashedValueWithO15 = password_hash($password, PASSWORD_BCRYPT, $options15);

echo "1hashedPassword:".$hashedValue;
echo "<br>2hashedPassword:".$hashedValueWithO12;
echo "<br>3hashedPassword:".$hashedValueWithO15;

/*
if(password_verify($password, $hashedValue))
echo "<br>1true";
else
echo "<br>1false";
if(password_verify($password, $hashedValueWithO12))
echo "<br>2true";
else
echo "<br>2false<br>";

if(password_verify($password, $hashedValueWithO15))
echo "<br>3true";
else
echo "<br>3false<br>";
*/
echo "<br>1.hashedValue:";
print_r( password_get_info( $hashedValue ));

echo "<br>2.hashedValue:";
print_r( password_get_info( $hashedValueWithO12 ));

echo "<br>3.hashedValue:";
print_r( password_get_info( $hashedValueWithO15 ));

?>




cost => 10



cost => 12



cost => 15




reference to : http://dolgo.net/PHP/lecture/18


'프로그래밍 > PHP' 카테고리의 다른 글

php CURL 사용하기  (0) 2018.07.31
Javascript로 Facebook로그인  (0) 2018.05.26
unlink(=delete) 메소드 이용하기  (0) 2018.04.22
directory entry 읽어 파일리스트업  (0) 2018.04.20
파일 업로드(move_uploaded_file)  (3) 2018.04.14

업로드된 파일을 삭제하고 싶을 때 , unlink 메소드를 사용한다.


bool unlink ( string $filename [, resource $context ] )


인수

filename : Path to the file.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.


 

php초보자로 이벤트(클릭)를 발생시키고 파일을 삭제하는 것도 쉽지 않네.

 

php에서 파일삭제 메소드를 사용하기 위해서 아래 순서대로 호출.

크게 나쁘진 않은 것 같다. 보안관련 이슈는 차후에 학습하면서 추가하면 되겠다.

 

1)    Html a tag 사용한다.

2)    A tag에 삭제파라미터 : 파일명 를 넣고 해당 페이지를 호출

3)    Php get파라미터로 대상을 받아 파일삭제 메소드 호출


>> entries.PNG 삭제 버튼을 클릭함



>>bootstrap css 적용위한 css파일 첨부

bootstrap.min.css

light-bootstrap-dashboard.css



php source : 


<?php
$dir = $_SERVER['DOCUMENT_ROOT']."/admin1/test/img/";


if ( isset ( $_GET['delete']) ) {
echo $_GET['delete'];
echo "<br>";

if ( unlink ( $dir. $_GET['delete'] ) ) {

header("Location: ./fileupload0.php");
exit;
};
}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fileupload</title>
<!-- Fonts and icons -->
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700,200" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css" />
<!-- CSS Files -->
<link href="../../admin3/assets/css/bootstrap.min.css" rel="stylesheet" />
<link href="../../admin3/assets/css/light-bootstrap-dashboard.css?v=2.0.1" rel="stylesheet" />

</head>
<body>
<div class="wrapper">
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<div class="card card-tasks">
<div class="card-header ">
<h4 class="card-title">Uploaded File's info</h4>
<p class="card-category">files이 있는 directory의 모든 파일을 읽어 리스트업</p>
</div>
<div class="card-body ">
<div class="table-full-width">
<table class="table">
<tbody>
<?php
/**
* Description.
*
* @author: JinMyung <dcan@doitforyou.co.kr>
* @category Filesystem
* @version 1.0
*/

/* files이 있는 directory의 모든 파일을 읽어 리스트업 */
// $dir = $_SERVER['DOCUMENT_ROOT']."/admin1/test/img/";

// 핸들 획득
$handle = opendir($dir);
$files = array();
// 디렉터리에 포함된 파일을 저장한다.
while (false !== ($filename = readdir($handle))) {
if ($filename =="." || $filename =="..") {
continue;
}

// 파일인 경우만 목록에 추가한다.
if (is_file($dir . "/" . $filename)) {
$files[] = $filename;
}
}
// 핸들 해제
closedir($handle);

sort($files);

// 파일명을 출력한다.
foreach ($files as $f) {
//echo $f . "(" . round(filesize($dir . "/" . $f)/1024, 2) . " KB)" ;
$FileFullPathName = "";
$FileFullPathName = $dir . "/" . $f;
echo "<tr>";
echo "<td>".$f . "(" . round(filesize($FileFullPathName)/1024, 2) . " KB)(".date("Y-m-d H:i:s", filemtime($FileFullPathName)) .")</td>";
echo "<td class=\"td-actions text-right\">";
echo " <a href=\"?delete=". $f."\">"."<button type=\"button\" style=\"cursor:hand\" rel=\"tooltip\" title=\"Remove\" class=\"btn btn-danger btn-simple btn-link\">";
echo "<i class=\"fa fa-times\"></i>";
echo "</button></a>";
echo "</td>";
echo "</tr>";

}
?>

</tr>
</tbody>
</table>
</div>
</div>
<div class="card-footer ">
<hr>
<div class="stats">
<i class="now-ui-icons loader_refresh spin"></i>
<?php
echo "refer to : http://blog.devez.net/292 | http://php.net/manual/en/function.readdir.php";
?>
</div>
<form enctype="multipart/form-data" action="fileupload1.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="4000000" />
Send File: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

</div>
</div>
</div>
</div>
</div>
</div>
</div>

</body>
</html>


'프로그래밍 > PHP' 카테고리의 다른 글

Javascript로 Facebook로그인  (0) 2018.05.26
php 패스워드 암호화 bcript  (0) 2018.05.02
directory entry 읽어 파일리스트업  (0) 2018.04.20
파일 업로드(move_uploaded_file)  (3) 2018.04.14
fileupload 에러메시지 설명  (0) 2018.04.14

[영단어] directory entry 

           디스크에 있는 각 파일의 이름·크기·특징 등을 기재한 것

Reference url : http://dic.daum.net/search.do?q=entry



(PHP 4, PHP 5, PHP 7)

readdir — Read entry from directory handle


Description

string readdir ([ resource $dir_handle ] )

Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem.


reference url : http://php.net/manual/en/function.readdir.php



<?php
/**
* Description.
*
* @author: JinMyung <dcan@doitforyou.co.kr>
* @category Filesystem
* @version 1.0
*/

/* files이 있는 directory의 모든 파일을 읽어 리스트업 */
echo "<pre>";
echo "<b>files이 있는 directory의 모든 파일을 읽어 리스트업</b>";
echo "<br>";

$dir = $_SERVER['DOCUMENT_ROOT']."/admin1/test/img/";

// 핸들 획득
$handle = opendir($dir);
$files = array();
// 디렉터리에 포함된 파일을 저장한다.
while (false !== ($filename = readdir($handle))) {
if ($filename =="." || $filename =="..") {
continue;
}

// 파일인 경우만 목록에 추가한다.
if (is_file($dir . "/" . $filename)) {
$files[] = $filename;
}
}
// 핸들 해제
closedir($handle);

sort($files);

// 파일명을 출력한다.
foreach ($files as $f) {
echo $f . "(" . round(filesize($dir . "/" . $f)/1024, 2) . " KB)" ;
echo "<br />";
}

echo "</pre>";
echo "reference url : http://blog.devez.net/292 | http://php.net/manual/en/function.readdir.php";

?>




'프로그래밍 > PHP' 카테고리의 다른 글

php 패스워드 암호화 bcript  (0) 2018.05.02
unlink(=delete) 메소드 이용하기  (0) 2018.04.22
파일 업로드(move_uploaded_file)  (3) 2018.04.14
fileupload 에러메시지 설명  (0) 2018.04.14
angularJS 사용하기  (0) 2018.04.01

+ Recent posts