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);
Related
I want to send a data using curl and return some data
I'am using the following code
$data = array('type' => 'active');
$result = '';
$url = 'https://example.com/'.$function_name;
$send_data = json_encode($data, TRUE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $send_data);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);exit;
On the receiving end returning the POST data
print_r($_POST);exit;
But I'am getting data in different structure as below
(
[{"type":"active"}] =>
)
I think due to this $_POST['type'] is not getting in the receiving side
I am trying to fetch data from webservice(nodejs) using curl in php but i am getting result "lang is required"
i tried with following code but not working for me where i am wrong ?
Here is my code
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="http://xxxxxx:8000/api/employer/login";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
Usually to send post variables i use : the http_build_query function.
$url = "http://xxxxxx:8000/api/employer/login";
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
It works better.
first check the request whether send post field or not.
$post = ['lang'=> "593f973dea53161779dd5660",'password'=> "amit123d"];
$ch = curl_init();
$url="https://httpbin.org/post";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$result = json_decode($response);
echo "<pre>";print_R($result);
the site: https://httpbin.org/ will response the post field if the post data exists.
I think the problem may occurs the nodejs side. Does it check the post field rightly ?
from my experience your code is right.
I want to send some data to another server but I don't want user to view the data.
I tried submitting form but others still manage to see the data even it is hidden.
PS: The server only receives $_POST.
You use curl for this purpose.
An example:
<?PHP
function POST($url,$data,$headers,$type)
{
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_POST, 1);
if($type === 1)
{
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
}
else
{
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$output = curl_exec ($ch);
$posturl = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$cURLinfo = curl_getinfo($ch);
curl_close($ch);
return array(
"output" => $output,
"posturl" => $posturl,
"httpcode" => $httpCode,
"diagnostics" => $cURLinfo
);
}
?>
But most likely, this is a duplicate of How do I send a POST request with PHP?
use forge.js to encrypt the data in base64 e.g.
jQuery.post('yourfile.php',
{uname: forge.util.encode64(jQuery('#uname').val()) ,
upass: forge.util.encode64(jQuery('#upass').val() )
},function(data)
{
console.log(data);
});
and then in PHP you can use
$user = base64_decode($_POST['uname']);
$passw = base64_decode($_POST['upass']);
to retrieve the data.
I have this code in my curl:
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'User-Agent: '.$_SERVER['HTTP_USER_AGENT'],
'X-Forwarded-For: '.$this->getIp()
));
$result = curl_exec($ch);
return $result;
I am getting this giberish result:
I am expecting an image file.
Question here is that how can i download this image file?
you can use this code
$profile_Image = $url; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = ''; // your saving path
$ch = curl_init($profile_Image);
$fp = fopen($path . $userImage, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
you can try this:
$data=send_curl($url);
$path='./test.jpg';
$file = file_put_contents($path,$data);
the function of send_curl
function send_curl($url, $data, $PROXY = "") {
$ch = curl_init ();
if ($PROXY != "") {
curl_setopt ( $ch, CURLOPT_PROXY, $PROXY );
}
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
Ideally, browsers decide how to display a given piece of content using the information of the Content-Type HTTP header and PHP can generate HTTP headers with the header() function.
If you know for sure that your picture will always be in JPEG format it's rather straightforward:
header('Content-Type: image/jpeg');
echo your_download_function();
exit;
Otherwise, you need to fetch the header returned by the remote server. Curl has a tricky syntax—I refer you to PHP cURL retrieving response headers AND body in a single request?
Using the code below, I'm unable to send any POST data. The page that is supposed to receive it, isn't receiving it for some reason..
$url = "http://www.somesite.com/curl_test_page.php";
$employee_id = 5;
$post_fields = "employee_id=" . $employee_id;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
On the curl_test_page.php, the code is as follows:
$employee_id = $_POST['employee_id'];
echo $employee_id;
When I run the CURL script, it displays an empty page.
You are not using curl_init
Is recomended force the enctype with CURLOPT_HTTPHEADER
I recomend you use http_build_query function
Example:
$url = "http://www.somesite.com/curl_test_page.php";
$employee_id = 5;
$post_fields = Array(
employee_id => $employee_id
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_fields));
$result = curl_exec($ch);
You are not printing any output. Try adding
<?php
echo $result;
?>