0

I am trying to POST a file from URL using PHP cURL to the another server and getting an error:

There was error in processing your request.

Here is my code:

$url = 'http://www.example.com/api/uploads';
$fields = array('id' => $apikey,'token' => $token ,'myFile' => array(urlencode(base64_encode(file_get_contents("http://www.pdf995.com/samples/pdf.pdf")))));
$data_json = json_encode($fields);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', 'cache-control: max-age'));
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
2
  • Please give the error, it would be helpful to understand... Commented Nov 3, 2016 at 14:12
  • Does the server receiving the upload accept POST requests from the remote URL? In other words, does it have the appropriate CORS settings? Commented Nov 3, 2016 at 14:14

1 Answer 1

1

Field should be like this if you want to upload a file.

// file from your local directory
$fields = array('id' => $apikey,'token' => $token ,'myFile' => "@/samples/pdf.pdf");

And the post fields should be:

curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);

Remove the option CURLOPT_HTTPHEADER.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.