Wednesday, October 26, 2022

[SOLVED] PHP Curl: failed creating formpost data

Issue

Where ever and on each site I look, they say wrong file path cause this error, But I am sure my path is correct and it works last night why not tonight??!!

To prove that the path is correct, file_exists($path) return true.

I want to send file using Curl and I get failed creating formpost data. The path to file which I give to Curl is like below:

define('ASSET_PATH', dirname(dirname(dirname(__FILE__))) . '/asset');

$postfields = array(
    'chat_id'   => $user->user_id,
    'caption'   => $asset->caption,
    'duration'  => $asset->duration,
    'video'     => "@" . ASSET_PATH . "/video/" . $asset->name,
);

And when you see the value of $postfields['video'], the output is:

/home/hosseins/domains/my_domain/public_html/telegram/asset/video/Bottle_Bank.mp4

Further Code

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

$result = curl_exec($ch);
echo curl_error ( $ch ); //return failed creating formpost data
echo '<br />' . ASSET_PATH . "/video/" . $asset->name; //return named path
curl_close($ch);

Is there anything wrong with my addressing or problem coming from some where else?
Dudes I'm stuck here, any help will be appreciated.
Thanks in advance


Solution

Curl treat @ as path to file, means when you use @ at first of string, that string considered as file path. So here Curl think my caption is path to file and he can't find that absolutely.



Answered By - Hossein Shahsahebi
Answer Checked By - David Goodson (WPSolving Volunteer)