개발 Q&A

제목 curl 관련 질문했던 사람입니다
글쓴이 ParkHeeJi 작성시각 2015/02/25 12:36:21
댓글 : 3 추천 : 0 스크랩 : 0 조회수 : 17616   RSS
컨트롤러에서 curl 처리해서 뷰에서 버튼 누르면 네이버 검색결과창이 뜨는것까지는 했는데
에이젝스로 검색결과창 스크립트 부분 받아올때 alert(data)를 쳐보면 
<!doctype html> ~~~~~~~~ <meta name="description" lang="ko" content="''의 네이버 통합검색 결과입니다"><title> : 네이버 통합검색</title>~~~</script></body></html>

위의 [''의 네이버 통합검색 결과입니다]에서 검색어가 적용이 안돼서 들어가있더라구요
예를들어서 뷰 인풋창에 '자동차'라고 치고 버튼을 누르면 컨트롤러에서 처리하고 에이젝스로 데이터를 받아올때
['자동차'의 네이버 통합검색 결과입니다] 라고 나와야 연관검색어 추출이 돼잖아요

컨트롤러 함수 소스는

$test = $this -> input -> post('test');
$url = "http://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&ie=utf8&query=".$test;
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';

        $request_headers = array();
        $request_headers[] = 'User-Agent: '. $User_Agent;
        $request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
        $cookies = 'CookieName1=Value;CookieName2=Value';
        $request_headers[] = 'Cookie: '. $cookies;
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt ($ch, CURLOPT_USERAGENT, $User_Agent);
        curl_setopt ($ch, CURLOPT_COOKIE, $cookies);

        $file_contents = curl_exec($ch);
        echo $file_contents;

뷰에서 검색했을때 네이버 검색결과창이 전체적으로 다 뜨구요 에이젝스에서 데이터(스크립트) 받아올때 검색결에 변수가 안 먹혀서 들어옵니다 답변 기다릴게요
 다음글 php curl 파싱 질문 좀 드릴게요 (2)
 이전글 중국에 서비스 하려고 하는데 궁금한게 있습니다 ^^.. (4)

댓글

한대승(불의회상) / 2015/02/25 15:00:10 / 추천 0
네이버 open api를 사용하는게 나을것 같은데요.
아래 URL 확인해 보세요.
http://developer.naver.com/wiki/pages/SrchAPI
변종원(웅파) / 2015/02/25 18:31:50 / 추천 0
api가 있는 것을 굳이 파싱할 필요는 없어 보이는데요.
실력검증인가요?
ci세상 / 2015/02/25 22:21:47 / 추천 0

api로 하시는것이 가장 좋구요~~파싱을 직접 하실경우 몇가지 팁을 드리면요 ~

Tip1> 문자열 시작과 끝 자르기
http://php.net/manual/en/function.strpos.php
function zGetText($str, $startChar, $endChar) {
 if(!$startChar) $start = 0;
 else $start = strpos($str, $startChar)+strlen($startChar);
 $tmp = substr($str, $start, strlen($str));

 if(!$endChar) $end = strlen($str); 
 else $end = strpos($tmp, $endChar);
 $result = substr($tmp, 0, $end);

 return $result;
}

$file_contents = zGetText($file_contents,"<dd class=\"lst_relate\">","</dd>");


Tip2> Curl Header / Curl Proxy등
http://php.net/manual/en/book.curl.php

Tip3> Ajax 예제
https://developer.mozilla.org/ko/docs/AJAX/Getting_Started