2012-03-12 116 views
2

我是编程中的新人。我想保存来自Google图片的图片。我只能使用过滤器免费的。我使用Google API,我有URL,我想将它传递给PHP,但我不知道如何将它从JavaScript传递到PHP。我尝试使用表格,并且document.write('<a href="prueba4.php?url1=unescapedUrl">save the image</a>');但它不起作用。如何保存来自Google的图像

function MyKeepHandler(result) { 
    // clone the result html node 
    var node = result.html.cloneNode(true); 
    // attach it 
    var savedResults = document.getElementById("content"); 
    savedResults.appendChild(node); 
    // extract some info from the result to show to get at the individual attributes. 
    // see http://code.google.com/apis/ajaxsearch/documentation/reference.html 
    var title = result.title; 
    var unformattedtitle = result.titleNoFormatting; 
    var content = result.content; 
    var unescapedUrl = result.unescapedUrl; 

    document.write('<a href="prueba4.php?url1=unescapedUrl">save the image</a>'); 
    // alert("Saving " + unformattedtitle + " " + unescapedUrl + " " + content); 
    var_dump($x); 
} 

function OnLoad() { 
    // Create a search control 
    var searchControl = new google.search.SearchControl(); 
    // Add in a WebSearch 
    var webSearch = new google.search.WebSearch(); 
    // Add in a full set of searchers 
    searchControl.addSearcher(new google.search.ImageSearch()); 
    //var localSearch = new google.search.LocalSearch(); 
    // tell the searcher to draw itself and tell it where to attach 
    searchControl.draw(document.getElementById("content")); 
    searchControl.setOnKeepCallback(this, MyKeepHandler); 
    // execute an inital search 
    searchControl.execute("tomates"); 
} 

google.setOnLoadCallback(OnLoad); 
</script> 

</head> 
<body style="font-family: Arial;border: 0 none;"> 
<div id="content">Loading...</div> 
</body> 
</html> 

为了节省我使用这个图像(这个作品,如果我用URL名称):

function saveImage($url,$path) { 
    $c = curl_init(); 
    curl_setopt($c,CURLOPT_URL,$url); 
    curl_setopt($c,CURLOPT_HEADER,0); 
    curl_setopt($c,CURLOPT_RETURNTRANSFER,true); 
    $s = curl_exec($c); 
    curl_close($c); 
    $f = fopen($path, 'wb'); 
    $z = fwrite($f,$s); 
    if ($z != false) return true; 
    return false; 
} 
saveImage($url1,$path); 
+0

+1为是“永远当家” – 2012-03-12 09:12:41

回答

0

一件事我注意到:

document.write('<a href="prueba4.php?url1=unescapedUrl">save the image</a>'); 

没有变量代换在JavaScript中,所以这将输出确切的文字。将其更改为:

document.write('<a href="prueba4.php?url1=' + unescapedUrl + '">save the image</a>'); 
+0

非常感谢您的回答,你是对的,但我不能URL名称传递给PHP。它说,不存在url1变量。 “注意:未定义的变量:在第16行的C:\ server \ www \ imagenes \ prueba4.php中的url1” – paulaandrea 2012-03-12 09:41:57

+0

将'$ url1'更改为'$ _GET ['url1']' – 2012-03-12 09:54:11

+0

Martin Dimitrov感谢这么多!最后它的作品!我太高兴了。你是最好的,今天我的祈祷是为你;) – paulaandrea 2012-03-12 10:10:29