2014-02-05 150 views
0

這是我的問題,自動執行提交操作:如何在外部網站

每天我訪問一個網站(當然,登錄)並上傳相同的圖像。 我想知道是否有辦法自動化這個過程。

我希望有人幫助我:) 感謝您的幫助!

回答

0

您可以嘗試通過curl提交文件。

$file_name_with_full_path = realpath('./sample.jpeg'); 
$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$result=curl_exec ($ch); 
curl_close ($ch); 

並登錄到特定的網站,並保持Cookie,可以使一個類似的功能:

public static function curlResult ($url, $getHeaders = false, $data) { 

    $options = array(
     CURLOPT_RETURNTRANSFER => true,  // return web page 
     CURLOPT_HEADER   => false, // return headers 
     CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
     CURLOPT_ENCODING  => "",  // handle all encodings 
     CURLOPT_USERAGENT  => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1", 
     CURLOPT_AUTOREFERER => true,  // set referer on redirect 
     CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect 
     CURLOPT_TIMEOUT  => 120,  // timeout on response 
     CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 
     CURLOPT_COOKIESESSION => true, 
     CURLOPT_COOKIEJAR  => dirname(__FILE__) . '/cookie.txt', 
     CURLOPT_COOKIEFILE  => dirname(__FILE__) . '/cookie.txt' 
    ); 

    $ch = curl_init($url); 
    curl_setopt_array($ch, $options); 

    if ($data != false) { 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($ch, CURLOPT_POST, $data?1:0); 
    } 

    $content = curl_exec($ch); 
    $header = curl_getinfo($ch); 
    curl_close($ch); 

    if ($getHeaders) { 
     return $header; 
    } 

    return $content; 
} 

然後使用功能類似:

// Login 
curlResult('urltologin', false, array('username' => 'test', 'password' => 'demo'); 
// Upload 
curlResult('urltoupload', false, array(file_contents'=>'@'.$file_name_with_full_path)); 
0

另外,您可以使用Selenium(通過他們自己的Java Client):

取自here

import junit.framework.Assert; 
import junit.framework.TestCase; 
import org.openqa.selenium.*; 
import org.openqa.selenium.remote.*; 
import java.net.URL; 
import java.util.concurrent.TimeUnit; 
    public class TestingUploadSe2Sauce extends TestCase { 
    private RemoteWebDriver driver; 
    public void setUp() throws Exception { 
      DesiredCapabilities capabillities = DesiredCapabilities.firefox(); 
      capabillities.setCapability("version", "7"); 
      capabillities.setCapability("platform", Platform.XP); 
      capabillities.setCapability("selenium-version", "2.18.0"); 
      capabillities.setCapability("name", "Remote File Upload using Selenium 2's FileDetectors"); 
     driver = new RemoteWebDriver(
      new URL("http://<username>:<api-key>@ondemand.saucelabs.com:80/wd/hub"), 
     capabillities); 
     driver.setFileDetector(new LocalFileDetector()); 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
} 

public void testSauce() throws Exception { 
     driver.get("http://sso.dev.saucelabs.com/test/guinea-file-upload"); 
     WebElement upload = driver.findElement(By.id("myfile")); 
     upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg"); 
     driver.findElement(By.id("submit")).click(); 
     driver.findElement(By.tagName("img")); 
     Assert.assertEquals("darkbulb.jpg (image/jpeg)", driver.findElement(By.tagName("p")).getText()); 
} 
public void tearDown() throws Exception { 
     driver.quit(); 

} 
}