簡體   English   中英

PHP 卷曲圖片上傳

[英]PHP Curl Image Upload

如何通過cURL實現圖片上傳?

有人告訴我使用 curl_file_create 但它不起作用。

這是當我手動(使用 Chrome)上傳文件並查看原始結構時來自 post 的數組:

Array
(
    [image] => Array
        (
            [0] => 377529090726537.jpg
        )

    [thumbnail_digest] => Array
        (
            [0] => EMPTYDIGEST
        )

    [digest_present] => Array
        (
            [0] => 0
        )

    [image_rotation] => Array
        (
            [0] => 0
        )
)

在我的 cURL 中,我嘗試過:

$cfile = curl_file_create('IMG_33071.jpg','image/jpeg','test_name');
$post_data = array(
'image' => $cfile,
);

但是在運行腳本后,我收到此錯誤:

可捕獲的致命錯誤:CURLFile 類的對象無法在第 144 行的 C:\\Path\\to\\my\\server\\curl.php 中轉換為字符串

該圖像位於 curl.php 文件的同一文件夾中。 我的 php 版本是 5.5。

我也試過這個:

$cfile = curl_file_create('http://www.siteexample.com/path/to/my/file.jpg','image/jpeg','test_name');

但沒有成功:(

我的結構:

//These are the post data username and password
$login_data = 'email='.$email.'&password='.$password.'&entrar=Entrar';

//Create a curl object
$login = curl_init();

//Set the useragent
$agent = $_SERVER["HTTP_USER_AGENT"];
//$agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
curl_setopt($login, CURLOPT_USERAGENT, $agent);

curl_setopt($login, CURLOPT_SSL_VERIFYPEER, false);

//Set the URL
curl_setopt($login, CURLOPT_URL, $login_url );

//This is a POST query
curl_setopt($login, CURLOPT_POST, 1 );

//Set the post data
curl_setopt($login, CURLOPT_POSTFIELDS, $login_data);

//We want the content after the query
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);

//Follow Location redirects
curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);

/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/

curl_setopt($login, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($login, CURLOPT_COOKIEFILE, 'cookie.txt');

//Execute the action to login
$LoginResult = curl_exec($login);
echo $LoginResult;



    //create array of data to be posted
$post_data = array(
'name' => 'Name',
'email' => $email,
'email_confirm' => $email,
'phone' => '9999999999',
'passwd' => $password,
'passwd_ver' => $password,
'create' => '',
);


//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}


//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

var_dump($post_string);

curl_setopt($login, CURLOPT_URL, 'http://urltocurl.com/verify');
curl_setopt($login, CURLOPT_POST, false);
curl_setopt($login, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($login, CURLOPT_TIMEOUT, 0);
$answer = curl_exec($login);
echo $answer;


if (curl_error($login)) {
    echo curl_error($login);
}

//close the connection
curl_close($login);

現在看看開始處的數組。

Array
    (
        [image] => Array
            (
                [0] => 377529090726537.jpg
            )

它是另一個數組中的數組。 如何做到這一點? 也許 curl_file_create 正在工作,但它將圖像發送到錯誤的數組位置。

看看我的例子,它應該適合你:

$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/path/to/image.jpeg');
curl_setopt($ch, CURLOPT_URL, 'http://example.com/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM