André Figueira André Figueira -4 years ago 140
PHP Question

cURL not sending file to API CALL

I am building a mobile site that runs from an API and have an API CALL handler class which does all the calls which I run from a primary functions file.

The issue here is my files are not being sent through to the API and it's not recognising what is a file and is returning a file not present error.

NOTE: ISSUE SOLVED AND WORKING CODE BELOW

Code Below:

FORM

<form id="uploadPhoto" action="<?php uploadStreamPhoto(); ?>" method="post" enctype="multipart/form-data">
<input type="file" name="streamPhotoUpload" id="streamPhotoUpload" />
<input type="submit" name="streamPhotoUploadSubmit" id="streamPhotoUploadSubmit" value="Upload" />
</form>


UPLOAD FUNCTION

function uploadStreamPhoto()
{

if(isset($_POST['streamPhotoUploadSubmit']))
{

$apiHandler = new APIHandler();
$result = $apiHandler->uploadStreamPhoto($_FILES['streamPhotoUpload']['tmp_name']);
$json = json_decode($result);
var_dump($json);

//header('Location: '.BASE_URL.'stream-upload-preview');

}

}


HANDLER METHOD

public function uploadStreamPhoto($file)
{

$result = $this->request(API_URL_ADD_PHOTO, array(
'accessToken' => $this->accessToken,
'file' => "@$file;filename=".time().".jpg",
'photoName' => time(),
'albumName' => 'Stream'
));

return $result;

}


CURL REQUEST METHOD

/**
* Creates a curl request with the information passed in post fields
*
* @access private
* @param string $url
* @param array $postFields
* @return string
**/
private function request($url, $postFields = array())
{

$curl = curl_init();

//Check the SSL Matches the host
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

if($this->debug == true)
{

//Prevent curl from verifying the certificate
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

}

//Set the URL to call
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);

//Set the results to be returned
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

//Set the curl request as a post
curl_setopt($curl, CURLOPT_POST, 1);

//Set the post fields
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);

$result = curl_exec($curl);

if($result === false)
{

$result = 'Curl error: '.curl_error($curl);

}

curl_close($curl);

return $result;

}

Answer Source

OK I have found out what the issue was, hopefully the solution will help a lot of people who don't want to change how their code is in lieu of someone elses.

cURL was not detecting that it was supposed to send this form as a multipart so it was sending the post as a default encoding meaning the other end wasn't receiving the $_FILES variable.

To solve this you need to give the postdata as an array, I was creating the string for the send, I have removed this and am giving the CURLOPT_POSTFIELDS an array.

Another important thing when uploading directly from a form using cURL is to include the information for your file along with the actual file.

My API Call handler now created the array as follows:

public function uploadStreamPhoto($file)
{

    $result = $this->request(API_URL_ADD_PHOTO, array(
    'accessToken' => $this->accessToken,
    'file' => "@$file;filename=".time().".jpg",
    'photoName' => time(),
    'albumName' => 'Stream'
    )); 

    return $result;

}

Take note that the $file variable is $_FILES['tmp_name'] You then also have to define the file name. I will be updating the question with the solution.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download