Czy tak powinno wyglądać wysyłanie plików przez curl?
function post_data2($site,$data,$reffer){ $datapost = curl_init(); $headers = http://www.php.net/array('Referer: '.$reffer, "Content-Type" => "multipart/form-data"); curl_setopt($datapost, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($datapost, CURLOPT_URL, $site); curl_setopt($datapost, CURLOPT_ENCODING, 'gzip, deflate'); curl_setopt($datapost, CURLOPT_TIMEOUT, 40000); curl_setopt($datapost, CURLOPT_HEADER, TRUE); curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers); curl_setopt($datapost, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($datapost, CURLOPT_POST, TRUE); curl_setopt($datapost, CURLOPT_POSTFIELDS, $data); curl_setopt($datapost, CURLOPT_COOKIEFILE, "cookie.txt"); http://www.php.net/ob_start(); $r = curl_exec ($datapost); http://www.php.net/ob_end_clean(); curl_close ($datapost); http://www.php.net/unset($datapost); return $r; } $file = http://www.php.net/array('file'=>'@'.http://www.php.net/realpath('plik').';filename=plik',"name"=>"FFFXXX@plik"); post_data2("......",$file,'......'.http://www.php.net/substr($htmltest->find('input[id=ID_ADD]')[0]->onclick,22,-2));
Cześć,
napisałem działający kod:
curl.php
/** * @author aras785 * @param string $url * @param array $data * @param string $referer * @return string */ function sendPostDataUsingCurl(string $url, http://www.php.net/array $data, string $referer): string { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); curl_setopt($ch, CURLOPT_TIMEOUT, 40000); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Referer: ' . $referer, 'Content-Type' => 'multipart/form-data']); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $response = curl_exec($ch); curl_close($ch); return $response; } $file = new CurlFile('zdjecie.jpg'); http://www.php.net/echo sendPostDataUsingCurl('https:/example.com/index.php', ['image' => $file], 'https://wp.pl');
<?php if(http://www.php.net/isset($_FILES['image'])){ $errors= http://www.php.net/array(); $file_name = $_FILES['image']['name']; $file_size =$_FILES['image']['size']; $file_tmp =$_FILES['image']['tmp_name']; $file_type=$_FILES['image']['type']; $file_ext=http://www.php.net/strtolower(http://www.php.net/end(http://www.php.net/explode('.',$_FILES['image']['name']))); $extensions= http://www.php.net/array("jpeg","jpg","png"); if(http://www.php.net/in_array($file_ext,$extensions)=== false){ $errors[]="extension not allowed, please choose a JPEG or PNG file."; } if($file_size > 2097152){ $errors[]='File size must be excately 2 MB'; } if(http://www.php.net/empty($errors)==true){ http://www.php.net/move_uploaded_file($file_tmp,"upload/".$file_name); http://www.php.net/echo "Success"; }else{ http://www.php.net/print_r($errors); } } ?> <html> <body> <form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="image" /> <input type="submit"/> </form> </body> </html>
Sam formularz działa, curl natomiast nie . Ciekawe czy ja coś z xamppem mam bo ogólnie curl działa mogę pobrać cały html i wyciągnąć dane tylko problem mam z wysyłaniem plików.
Hmm... to sprawdź czy jesteś w stanie wysłać plik na serwer z cmd: curl -F 'data=plik.txt' http://twoj-host
Wrzuć mój kod + obrazek zdjecie.jpg na serwer (pamiętaj maks 2mb) to Ci zadziała
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)