2016-09-10 3 views
1

몇 분 후에 실행되고 데이터를 가져 오는 스크립트를 개발했습니다. 내가 처음 로그인 한 다음 특정 링크에서 데이터를 가져와야 할 때마다. 난 단지 로그인 후 한 번만 로그인하고 매번 특정 링크에서 데이터를 가져올 수 있는지 알고 싶습니다.한 번 로그인 한 후 링크에서 데이터를 스크랩하는 방법

로그인 페이지가 자동 숨겨진 필드 이름을 생성 "TESTNAME"그래서 내가 먼저 로그인 페이지를 스크랩하고 숨겨진 필드를 얻기 위해 숨겨진 필드

을 긁어 로그인 페이지를 얻을.

<?php 
$url="http://www.example.com/login"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
$cookie = 'cookies.txt'; 
curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie); 
curl_setopt($ch, CURLOPT_COOKIEFILE,  $cookie); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
$content = curl_exec($ch); 

$result3 = preg_match('/<input type="hidden" name="testname" value=" (.*?)"/', $content, $matches); 

//Script to login the page after getting hidden field from login page and I want to login only once this code and do not login each time 

$fields=array('testname'=>$matches[1],'email' => 'username', 'password' => 'password','btnLogin'=>'login'); 

$url1 = "http://www.example.com/comapny/index.php"; 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url1); 
$timeout = 30; 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT,   10); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie); 
curl_setopt($ch, CURLOPT_COOKIEFILE,  $cookie); 
curl_setopt($ch, CURLOPT_REFERER, $url1); 
curl_setopt($ch,CURLOPT_POST, 1); 
curl_setopt ($ch,CURLOPT_POSTFIELDS,$fields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch); 

// After login I want to get the data from below link after every 10min 

$url1="http://www.example.com/abc/detail.php"; 
curl_setopt ($ch, CURLOPT_POST, 0); 
curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie); 
curl_setopt($ch, CURLOPT_COOKIEFILE,  $cookie); 
curl_setopt($ch, CURLOPT_URL, $url1); 
$result = curl_exec($ch); 

답변

2
//#Login and save data in cookie file 

    $curl=curl_init(); 
    curl_setopt($curl, CURLOPT_URL, "path"); 
    curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt"); 
    curl_setopt($curl, CURLOPT_HEADER, 0); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type:application/json")); 
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $Data); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 

    $result = curl_exec($curl); 

    //# Use cookie data using CURLOPT_COOKIEFILE 

    $curl=curl_init(); 

    curl_setopt($curl, CURLOPT_URL, "path"); 
    curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie.txt"); 
    curl_setopt($curl, CURLOPT_HEADER, 0); 
    curl_setopt($curl, CURLOPT_HTTPGET, TRUE); 
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflat'); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 

    $result = curl_exec($curl); 
관련 문제