I'm getting the response "failed creating formpost data" from curl whenever I try to upload a file. I'm sensing from other posts on the web that it's the filepath that's wrong, but I can't seem to get an absolute path on heroku.
When i do dirname(__FILE__) all I get is /app/www/ that can't be right can it? How do I get the absolute file path in a heroku app?
this is my entire line for the image path to be included in post data
"#".dirname(__FILE__).'/images/wallimages/'.random_pic()
and my curl setup:
$ch = curl_init('https://url.com/'.$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch))
{
return 'Curl error: ' . curl_error($ch);
}
curl_close ($ch);
return $result;
I suspect the root cause of your problem may be this line from the manual:
As of PHP 5.2.0, value must be an array if files are passed to this option with the # prefix.
In any case, try this code and if it doesn't work, let me know what error message(s) you get so I can debug:
$uploadPath = rtrim(dirname(__FILE__),'/').'/images/wallimages/'.random_pic();
if (!is_file($uploadPath)) die("The file '$uploadPath' does not exist");
if (!is_readable($uploadPath)) die("The file '$uploadPath' is not readable");
$postFields = array (
'nameOfFileControl' => "#$uploadPath",
'someOtherFormControl' => 'some value'
// Adjust this array to match your post fields.
// Ensure you keep the array in this format!
);
$ch = curl_init('https://url.com/'.$url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($ch);
if(curl_errno($ch))
{
return 'Curl error: ' . curl_error($ch);
}
curl_close ($ch);
return $result;
Related
Hi everyone i have crul request when i use it in single php page without laravel it work fine but if i put it inside laraevl controller to use it not working and return code status 0
My Request
$data = '{"PaymentInquiryV4RequestMessage":{"RefNum":"123456"}}';
$ch = curl_init('https://b2btest.stcpay.com.sa/B2B.DirectPayment.WebApi/DirectPayment/V4/PaymentInquiry');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);
curl_setopt($ch, CURLOPT_SSLCERT, 'crt.crt');
curl_setopt($ch, CURLOPT_SSLKEY, 'key.key');
curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-ClientCode: XXXXXXXXXXX'
)
);
$result = curl_exec($ch);
$result = json_decode($result, true);
print_r ($result);
when dealing with curl always start by debugging errors, which you can enable like
$result = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
echo $error_msg;
}
and as you mentioned in comment, you received below error after adding above code:
could not load PEM client certificate, OpenSSL error error:02001002:system library:fopen:No such file or directory, (no key found, wrong pass phrase, or wrong file format?)
It clearly indicate it can't find your certificate files, update path to those files in your call , like if your certificates are in directory /var/www/certificate/, your code would be
curl_setopt($ch, CURLOPT_SSLCERT, '/var/www/certificate/crt.crt');
curl_setopt($ch, CURLOPT_SSLKEY, '/var/www/certificate/key.key');
and make sure cert files are readable by setting proper permissions.
p.s you can also use Laravel helpers to get the path
base_path(); // '/var/www/mysite'
app_path(); // '/var/www/mysite/app'
storage_path(); // '/var/www/mysite/storage'
so you call will contain like
curl_setopt($ch, CURLOPT_SSLCERT, base_path().'/certificate/crt.crt');
curl_setopt($ch, CURLOPT_SSLKEY, base_path().'/certificate/key.key');
I am trying to post a file and some data using curl to a local go server hosted on the same machine:
// initialise the curl request
$request = curl_init('http://localhost:9049/api');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'company' => 'compname',
'user' => 'compuser',
'srv'=> 0,
'colTxt' => 'col1|col2|col3',
'upfile' => '#' . $_FILES['upfile']['tmp_name']
. ';filename=' . $_FILES['upfile']['name']
. ';type=' . $_FILES['upfile']['type']
));
// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($request);
// close the session
curl_close($request);
Unfortunately the above code is giving an empty response, have tried many various methods, but still an empty response.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
Even the curl_getinfo($ch) is empty, so it is getting hard to diagnose. Although there doesn't seem to be a network problem as i can post a file using curl from the command line and it worked perfectly fine. Any idea on what i might be missing?
Thanks.
Have a look here for details on using curl to upload files with the curlfile class
I want to access an XML file stored on another domain. The domain has an SSL certificate. I want to store it in my server folder. How can I achieve this in PHP? I am able to download without HTTPS from other sites, but unable to do it with HTTPS.
<?PHP
$dom = new DOMDocument();
$dom->load('www.123.com/abc.xml');
$dom->save('filename.xml');
?>
We are going to use Curl to download this file. There are 2 option.
The quick fix
The proper fix
The quick fix, first.
Warning: this can introduce security issues that SSL is designed to protect against.
set:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); to false
<?PHP
$url = "https://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml";
// create curl resource
$ch = curl_init();
//Now set some options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Download the given URL, and return output
$xml = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// Close the cURL resource, and free system resources
curl_close($ch);
// Write the string $xml to a file, data.xml
if (file_put_contents ('data.xml', $xml) !== false) {
echo 'Success!';
} else {
echo 'Failed';
}
?>
The second, and proper fix. Set 3 options:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\CAcert.crt');
The last thing you need to do is download the CA certificate.
Go to, - http://curl.haxx.se/docs/caextract.html -> click 'cacert.pem' -> copie/paste the text in to a text editor -> save the file as 'CAcert.pem' Check it isn't 'CAcert.pem.txt'
<?PHP
$url = "https://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml";
// create curl resource
$ch = curl_init();
//Now set some options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\CAcert.crt');
// Download the given URL, and return output
$xml = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// Close the cURL resource, and free system resources
curl_close($ch);
// Write the string $xml to a file, data.xml
if (file_put_contents ('data.xml', $xml) !== false) {
echo 'Success!';
} else {
echo 'Failed';
}
?>
I am trying to echo json of of a url. I can't figure out how to do it. I have tried many things. Can someone tell me why my code is not functioning properly? No json gets echoed to the screen. Thanks! Here is the link http://www.eastbay.com/shoppingcart/gateway?action=requestKey&_=
<?php
$raw_json = file_get_contents('http://www.eastbay.com/shoppingcart/gateway?action=requestKey&_=');
$json_array = json_decode($raw_json, true);
echo $json_array;
?>
Its not very pretty, you could put this in a loop but it wil get you started, I hope. Only thing you need to do is change the path where you want to store the cookies, it must be an absolute path.
<?php
$cookies = 'C:/folder/folder..../cookies.txt'; //use a absolute path
//Part 1 to get the cookies
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.eastbay.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Send Cookies.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); // Receive Cookies.
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
//part 2 to get the json
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.eastbay.com/shoppingcart/gateway?action=requestKey&_=");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); // Send Cookies.
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); // Receive Cookies.
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// Decoding the json in to an array
$decoded = json_decode($output, true);
//using the array to get the data
echo "RequestKey: " . $decoded['data']['RequestKey'] . "<br>";
echo "Success: ". $decoded['success'] . "<br>";
//foreach loop because its an array which hold the errors, it wont output anything because there are no errors.
foreach($decoded['errors'] AS $error){
echo "Error: " . $error . "<br>";
}
?>
I am attempting to send a file to an Https URL with this code:
$file_to_upload = array('file_contents'=>'#'.$target_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSER, FALSE);
curl_setopt($ch, CURLOPT_UPLOAD, TRUE);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'file='.$file_to_upload);
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close ($ch);
echo " Server response: ".$result;
echo " Curl Error: ".$error;
But for some reason I'm getting this response:
Curl Error: Failed to open/read local data from file/application
Any advice would help thanks!
UPDATE: When I take out CURLOPT_UPLOAD, I get a response from the target server but it says that there was no file in the payload
You're passing a rather strange argument to CURLOPT_POSTFIELDS. Try something more like:
<?
$postfields = array('file' => '#' . $target_path);
// ...
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
?>
Also, you probably want CURLOPT_RETURNTRANSFER to be true, otherwise $result won't get the output, it'll instead be sent directly to the buffer/browser.
This example from php.net might be of use as well:
<?php
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '#/home/user/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
On top of coreward's answer:
According to how to upload file using curl with php, starting from php 5.5 you need to use curl_file_create($path) instead of "#$path".
Tested: it does work.
With the # way no file gets uploaded.