I have a web-service that accepts file as a Post request and upload it on the server and returns the file path.
The service is working properly though Ajax request. But now i want to make a request from Php to the web service and its giving me error that "Undefined index: imageFile"
The code i am trying to use is below
$filename = 'e:/e.jpg';
$fields = array('imageFile'=>$filename);
$fieldsQueryString = http_build_query($fields);
// you should have curl library install here
$ch = curl_init("http://127.0.0.1/uploadapi/index.php/api/example/imageupload"); curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsQueryString);
curl_exec($ch);
You should add # (eta) sign before file path to upload it.
For more, read at PHP manual for curl_setopt for CURLOPT_POSTFIELDS setting.
<?php
$filename = '#e:/e.jpg';
$fields = array('imageFile'=>$filename);
$ch = curl_init("http://127.0.0.1/uploadapi/index.php/api/example/imageupload");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsQueryString);
curl_exec($ch);
I have to use
curl_file_create()
object to create the file and send it to the web service. Just did it and its working like a charm
Thanks
Related
I'm trying to send data via cURL post but I've never tried it before and I don't know if I'm doing it right.
What I want to do is sent a file via post to a file from my remote server and there read the file and insert data into database but unfortunately it's not working and error_log doesn't show me anything.
My code looks like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://".$host."/file.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file' => '#'.realpath(../path/to/file/'.$_POST['file_name'].'.txt'),
'action' => 'first',
'check' => $_POST['file_name'],
));
$result = curl_exec($ch);
curl_close($ch);
This code is placed after some sql querys and code made to write this sql results into the file that I'm trying to send.
Here is the Answer by Shakir Khan you Should follow:
PHP: upload file from one server to another server - Stack Overflow
OR you should read PHP Documentation carefully there is a huge amount of CONSTANT availabe to use: curl_setopt
I am using curl request to hit the has-offers conversion url from my sevrver with the help of curl but it is not working.But when I call the same URL using a browser, it works.Is they can block CURL requests?.I am not getting why, is there any port blocking issue.
Below is php code to call url using curl request.
<?php
function curl_get_contents($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url="http://paravey.go2cloud.org/aff_l?offer_id=12&aff_id=1000";
$contents = curl_get_contents($url);
echo $contents;
?>
Please help me thanks in Advance
The url you are curling is a pixel tracking url:
http://paravey.go2cloud.org/aff_l?offer_id=12&aff_id=1000
The aff_l endpoint looks for a cookie with session information (hence why it works in the browser).
If you want to create conversions with server side code, you will need to store the session identifier (the transaction_id) in your system and use the aff_lsr endpoint to send that data to HasOffers to trigger a conversion.
The url for this would look like this:
http://paravey.go2cloud.org/aff_lsr?transaction_id= VALUE
Where Value is the session identifier you have stored.
I would ask the HasOffers support team if you have more issues with this.
I have php file which consist of curl code so as to access an api. But when I upload this file on server and try to execute it via url no output is shown. Also file symbol is not shown as it is shown for php file. Guide me how to solve this problem. The code in php file is :
<?php // set up the curl resource
$ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch,CURLOPT_POSTFIELDS,"{}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, 'http://184.73.165.170:3000/api/3ace632b194e4366b821f7775b51cc4e/get-ibeacon-contents'); // execute the request
$output = curl_exec($ch);
// output the profile information - includes the header
//echo "curl response is :".($output). PHP_EOL; print($output);
// close curl resource to free up system resources
curl_close($ch); ?>
My server index shows as shown in image. As per my understanding a php file symbol is different than what it is shown in image I uploaded. Can anyone suggest about this ,if it helps me resolve my aim.
I tried on local and the cURL seems to work. If its working on local only and not on server then these are the things you should check:
Enable cURL on server ;extension=php_curl.dll and remove the semicolon; (uncomments) usually inside file php/php.ini
If you are working locally on Windows and trying to upload to a Linux server, then you might want to change EOL for UNIX/OSX Format. You can do this using Notepad++ by going to Edit -> EOL Conversion -> UNIX/OSX Format and then upload.
Try using your code this way
<?php
$json=json_encode(array('key1'=>'val1','key2'=>'val2'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://184.73.165.170:3000/api/3ace632b194e4366b821f7775b51cc4e/get-ibeacon-contents');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$json);
$output = curl_exec($ch);
var_dump(curl_getinfo($ch));
echo curl_errno($ch) . '-' . curl_error($ch);
curl_close($ch);
?>
I am trying to fetch file from the same server i am running my php script in which i am cUrl to fetch it.
It does not download file and get timeout.
I am able to get the file using same url from browser.
cUrl is able to get the file if the url is anything other than the same server.
Are their any settings i need to modify to support file download using cUrl on same server.
Appreciate your help here.
My code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $file_url);
$content = curl_exec($ch);
curl_close($ch);
try this with all parameters of curl
function curl_download($Url){
// is cURL installed yet?
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
// create a new cURL resource handle
$ch = curl_init();
// Now set some options (most are optional)
// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);
// Set a referer
//curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);
// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// Download the given URL, and return output
$output = curl_exec($ch);
// Close the cURL resource, and free system resources
curl_close($ch);
return $output;
}
Use Google Chrome' Copy as cURL (open "dev tools", "Network" tab, right-click on a request) on a successfully fetched file, paste to a terminal, then execute.
If everything is fine, and you are able to get a file via curl at terminal, then there is something with headers and/or request params. If not, it's more likely that something wrong with your network configuration.
Also, check web server logs, do you even get to a web server with your script?
Are you using 127.0.0.1 or localhost or full TLD? Is browser connecting to Internet via proxy?
The workaround I found is this:
Test if you are on your own server, then use a simple include for the same script:
if (($ser = servername()) != $floc) {
return(GetFileContent("$floc/$page.php"));
} else { include "$page.php"; }
I am trying to download a .exe file using PHP CURL from the web server to the PC, using CURL to make the webserver login automatic. The problem I'm having is that the file is being written to the browser window instead of asking the user to save or run the file. I'm using the code below, how can I fix this to make it work like I want?
$username = "myuser";
$password = "mypassword";
$url="http://www.mywebsite.com/files/softwaredownload/myfile.exe";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_exec ($ch);
curl_close($ch);
You should set the headers so the browser knows it has to display the save dialog.
Content-Disposition: attachment; filename=<file name.ext>
Also, bear in mind that Content-Type header should be before Content-Disposition.
Add to your code these lines:
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
... and replace curl_exec($ch) with $response = curl_exec($ch);, so $response content might be saved into a local file with file_put_contents() of fwrite() methods.