PHP cURL upload not working - php

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);

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.

Related

php post data using curl

When I post the data using ajax and send to post.php by curl then $_FILES variable is empty and get the data in $_POST variable. When I print $_POST var getting the following data on post.php.
[temp_upload_file] => #/tmp/php6OHgQc;filename=Penguins.jpg;type=image/jpeg
when I print "$_FILES" var getting empty data on post.php
Array
(
)
Code:
$url = "post.php";
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);
$data = array('temp_upload_file' =>'#'.$_FILES['uploadfile']['tmp_name'].';filename='.$_FILES['uploadfile']['name'].';type='. $_FILES['uploadfile']['type']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
What I am doing wrong here??
Thank you in advance
For php5.5+ there is a new curl_file_create() function you need to use
the # format is now deprecated.
Try using following code:
$url = "post.php";
$tmpfile = $_FILES['temp_upload_file']['tmp_name'];
$filename = basename($_FILES['temp_upload_file']['name']);
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'uploaded_file' => curl_file_create($tmpfile, $_FILES['uploadfile']['type'], $filename)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

how to read curl on remote server and reply

I want to send some data to remote server and wait for its reply,
i'm using curl to post data.
This is my client code:
$arr = array ('id' => $company , 'imei' => $drivers ,'startDate' => $startDate ,'endDate' => $endDate, 'token' => $token);
$string = http_build_query($arr);
$ch = curl_init("http://127.0.0.1/Codiad/workspace/PFE/S/server.php");
curl_setopt($ch, CURLOPT_POST , true);
curl_setopt($ch, CURLOPT_POSTFIELDS , $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$response = curl_exec($ch);
curl_close($ch);
The server should read $string, and do some checks based on received id, imei ...
and reply to client
here is what worked for me
1- sender.php:
function curlPost($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $result;
}
print(curlPost("http://127.0.0.1/receiver.php", array("message"=>"Hello World")));
2- receiver.php:
print("received ".$_REQUEST['message']);
or
print("received ".$_POST['message']);

unable to solve curl issue

I am new to using curl. I would like you to help me creating a code for this url in curl.
http://example.com/example/example.aspx?post1=xxx&post2=xxx&post3=xxx&post4=xxx&post5=xxxxxxxx&post6=xx&post7=xxxxxxx
I want to create a php file which has to get the similar data and post the data to above url using curl. Thanks for your help once again.
<?php
$url = "http://example.com/example/example.aspx";
$fields = array(
'post1' => 'xxx',
'post2' => 'xxx',
'post3' => 'xxx',
'post4' => 'xxx',
'post5' => 'xxx',
'post6' => 'xxx',
'post7' => 'xxx'
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
Try
$data = 'post1=xxx&post2=xxx&post3=xxx&post4=xxx&post5=xxxxxxxx&post6=xx&post7=xxxxxxx';
$ch = curl_init('http://example.com/example/example.aspx');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
For more :- Passing $_POST values with cURL
and manual which you need to read :- http://php.net/manual/en/book.curl.php

Camfind API issue in PHP

For anyone familiar with the Camfind API, I've got a bit of a stumper today Implementing the API in PHP. I've followed the instructions as best I could, and I'm getting the proper token back for my first call. I'm then appending the token to the URL, removing the form data, and keeping the json header, as well as the header with my key. Whatever I try, however, returns: array(1) { ["status"]=> string(13) "not completed" }
Here's my code:
<?php
///////////////////////////////////
// FIRST REQUEST
///////////////////////////////////
//set POST variables
$url = 'https://camfind.p.mashape.com/image_requests';
$fields = array(
"image_request[altitude]" => "27.912109375",
"image_request[language]" => "en",
"image_request[latitude]" => "35.8714220766008",
"image_request[locale]" => "en_US",
"image_request[longitude]" => "14.3583203002251",
"image_request[remote_image_url]" => "https://www.google.com/images/srpr/logo11w.png"
);
$headers = array(
'X-Mashape-Key: I-put-my-real-key-here',
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$result = json_decode($result, true);
///////////////////////////////////
// SECOND REQUEST
///////////////////////////////////
//set POST variables
$url = "https://camfind.p.mashape.com/image_responses/" . $result['token'];
echo $url;
$headers = array(
'X-Mashape-Key: I-put-my-real-key-here',
'Accept: application/json'
);
//open connection
$ch = curl_init();
//set the url
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
var_dump(json_decode($result, true));
Help appreciated!
Camfind support just got back to me, and made the following suggestion "Hi, it takes around 10-15 seconds for the image recognition to occur. Build in a loop with a delay before making the second call with the token, or try calling again and use a timer to re-call if not completed is still returned until it changes status to completed."
With this advice, I've updated my code as follows:
I added:
$start = microtime(true);
To the top of my file. I then wrapped my second curl call like so:
do {
//set the url
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 30);
$result = json_decode(curl_exec($ch), true);
} while((number_format((microtime(true) - $start), 2) < 20) && $result['status'] == 'not completed');
This is the solution, and the search now executes correctly.

curl POST error: The requested URL returned error: 413

I've looked around a bit and can't find a solution that solves my problem. I'm getting the following error: "The requested URL returned error: 413."
I'm posting some information via curl and PHP to a URL via HTTPS. I'm told that I have to pass the "Content-Length" along with my request since the destination is https and not http.
Here's the code I'm using:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$api = curl_exec($ch);
My strlen($data_string) command is returning a value of 5, which is much less than the actual length of the $data_string, which is much longer. I assume this is the problem unless someone thinks it might be being caused by something else.
Here's what I ended up with:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
if (!$ch) {
die("Couldn't initialize a cURL handle");
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$api = curl_exec($ch);
No SSL or size errors and it seems to work fine.

Categories