Hello stackoverflow community, I am having some problems when I try to send a request with curl in php, but something odd is that I have 2 servers, and when I use my code in one of them I get the response Im looking for, but in the other one I get the following response:
Http status code 500
As you can see it says "HTTP Status 500 - empty String", but why is that? in the other server I am not getting that with the same code
Thanks in advice to anyone who can help me out!
Ok mistake by no posting my code, here it is
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
curl_setopt($curl, CURLOPT_POST, 1);
//curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_POSTFIELDS, $string_data);
curl_setopt($curl, CURLOPT_TIMEOUT, 300);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
Related
When I run the below code on server it only shows the blank page and suddenly stop further execution, I also checked the cUrl on server which is installed.
Here is my code.
$ftp_server = 'ftps://'.'server/Voorraadtonen link.csv';
$ch = curl_init(str_replace(" ","%20",$ftp_server));
curl_setopt($ch, CURLOPT_URL, $ftp_server);
curl_setopt($ch, CURLOPT_USERPWD,'username'.':'.'password');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
//curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PORT, 990);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_setopt($ch, CURLOPT_VERBOSE,true);
$output = curl_exec($ch);
$error_no = curl_errno($ch);
echo $output; exit;
Latest update!
You have more than 1 errors in your codes,
you are using FTPS in url which requires SSL verification, and its false in
your codes.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//Dont use try! you shouldnt use
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
They should be true : SSL doesnt support true so they should be like following on #dharman warn in another answer.
But turning ssl true will require another setup like cacert file
etc. lik so
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//and include cacert.pem
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
Download cacert file here : https://curl.haxx.se/docs/caextract.html
2.Your url is not a true url $ftp_server = 'ftps://'.'server/Voorraadtonen link.csv';, this url will get nothing, but it should return an error atleast in error_log file, as you said all errors reporting are enabled
3.Your code should look like this
$curl = curl_init();
$file = fopen("link.csv", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://ftp.site.com/link.csv");
//Make sure for correct url
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
//Make sure for correct url
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//Make sure for your ftp credentials
curl_setopt($curl, CURLOPT_TIMEOUT, 20); //20 seconds will be enough
curl_exec($curl);
echo curl_errno($ch);
echo curl_error($ch);
curl_close($curl);
fclose($file);
1 more thing left headers should not be required but in case its required.
curl_setopt($curl, CURLOPT_HEADER, false); //Or
curl_setopt($curl, CURLOPT_HEADER, true);
Now it should work without any problem
NOTE : Example code is a working example you can edit it to your requirements
UPDATE : After modification you said you did in your codes (Still not showing us), finaly we get an error. once again I am asking you to add modified code into your question.
Error_no 28 cURL error 28: Connection timed out
the cURL 28 error occurs when the cURL request isn’t completed in a certain amount of time.
This happens when the cURL timeout value is set too low or when a firewall is blocking the cURL request.
Another possibility is a security module, for example the Apache mod_security module.
To fix the cURL error 28 you can contact your hosting provider.
So basicaly!
Your server is blocking. your credentials not match to required
credentails. SSL is required by server, but you are not setting it up.
Your function runing max of your Server Memory Limits settings.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "ftp.site.com/link.csv");
//make sure your path to file is correct
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
//make sure your login credentials correct
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
//Set timeout for connection
curl_exec($curl);
echo curl_errno($ch);
echo curl_error($ch);
//Get errors
curl_close($curl);
//Importand close curl connection.
I am so confused about my cURL script requesting the target page "facebook.com" for example, with the same port as the proxy.
example:
proxy is: 178.215.111.70:9999 . a cURL error says:
Failed connect to facebook.com:9999; Connection timed out
I see that it tries to connect to facebook using the poxy's port: 9999
Here is my code:
<?php
$curl = curl_init("https://facebook.com");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_REFERER, 'https://www.facebook.com');
if ($var) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, "test");
}
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT ,9999999);
curl_setopt($curl, CURLOPT_TIMEOUT, 0);
curl_setopt($curl, CURLOPT_PROXY, '178.215.111.70');
curl_setopt($curl, CURLOPT_PROXYPORT, '9999');
$result['exe'] = curl_exec($curl);
$result['err'] = curl_error($curl);
curl_close($curl);
echo $result['err'];
echo $result['exe'];
?>
I solved my problem.
It was a problem in the server only. When i tried the SAME script in another server it worked 100% fine
So always make sure of the configuration in your server when you face a problem like this.
I have installed curl. and configuration on my system is
cURL support enabled
cURL Information 7.35.0
Age 3
I have write code in my project is :
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_SSLVERSION,6);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_URL, $submit_url);
$curl_execute = curl_exec($curl);
curl_close($curl);
Its not giving me value on my local machine while working fine on server. What is problem?
My PHP version is
5.5.9-1ubuntu4.5
I am using ubuntu 13.10
what is problem with my local machine? what i am missing. please suggest what to do?
Edit :
If i missed any information to write, please inform me.. i am using curl very first time.
At first define the URL and post values correctly, I have added examples, please try this:
// define url examples
$submit_url = 'http://localhost/phptest/service.php';
// post fields examples
$postfields = 'name=foo&pass=bar&format=json';
// Start the process:
$curl = curl_init();
// Tell cURL to fail if an error occurs:
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
// Allow for redirects:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
// Assign the returned data to a variable:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Set the timeout:
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
// Use POST:
curl_setopt($curl, CURLOPT_POST, 1);
// Set the POST data:
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_URL, $submit_url);
// Execute the transaction:
$result = curl_exec($curl);
// Close the connection:
curl_close($curl);
// Print the results:
print_r($result);
cURL gives me the error:
Operation timed out after 0 milliseconds with 0 out of 0 bytes received
In particular, the "0 milliseconds" part is suspicious...
My initialization code:
$curl = curl_init($requestUrl); // private URL not published
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-Type: application/xml", "Accept: application/xml"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_HTTP200ALIASES, range(400, 599));
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
The timeout seems correctly set, what may be related it?
I had the same issue, when tried to connect via https. Problem was with ssl version.
This worked well for me:
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
I'm trying to use cURL to do facebook authentication and here's what I have:
$url = "https://graph.facebook.com/oauth/access_token";
$postString = "?client_id=$client_id&redirect_uri=$redirect_uri&client_secret=$client_secret&code=$code";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$response = curl_exec($curl);
but every time it just returns false.
I'm pretty new to using cURL so I could be making some beginner mistakes, but I'm confused as to why this isn't working at all.
Any help would be greatly appreciated!
This is probably too obvious and it may be a typo, but:
//curl_setopt($curl, CURLOPT_POST, 1);
Should be uncommented.
Remove the "?" from the beginning of your post string.. that should fix it!