curl_exec() always returns false
Error checking and handling is the programmer's friend. Check the return values of the initializing and executing cURL functions. curl_error()
and curl_errno()
will contain further information in case of failure:
try {
$ch = curl_init();
// Check if initialization had gone wrong*
if ($ch === false) {
throw new Exception('failed to initialize');
}
// Better to explicitly set URL
curl_setopt($ch, CURLOPT_URL, 'http://example.com/');
// That needs to be set; content will spill to STDOUT otherwise
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Set more options
curl_setopt(/* ... */);
$content = curl_exec($ch);
// Check the return value of curl_exec(), too
if ($content === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
// Check HTTP return code, too; might be something else than 200
$httpReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
/* Process $content here */
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
} finally {
// Close curl handle unless it failed to initialize
if (is_resource($ch)) {
curl_close($ch);
}
}
* The curl_init()
manual states:
Returns a cURL handle on success, FALSE on errors.
I've observed the function to return FALSE
when you're using its $url
parameter and the domain could not be resolved. If the parameter is unused, the function might never return FALSE
. Always check it anyways, though, since the manual doesn't clearly state what "errors" actually are.
PHP cURL returns FALSE on HTTPS
You can prevent cURL from trying to verify the SSL certificate by using CURLOPT_VERIFYPEER
.
Also set the action in the URL:
$ch = curl_init();
$data = array('user' => 'xxx', 'password' => 'yyy');
curl_setopt($ch, CURLOPT_URL, 'https://coinroll.it/getbalance');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
echo $result;
php curl request always return false
Since you're using private IPs and (most likely a dummy ssl certificate), you need to disable ssl verification for your requests.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
But be aware that this is a security risk. So avoid this in a production environment
cURL PHP RESTful service always returning FALSE
$response
is likely false because curl_exec()
returns false (i.e., failure) into $result
. Echo out curl_error($ch)
(after the call to curl_exec) to see the cURL error, if that's the problem.
On a different note, I think your CURLOPT_POSTFIELDS is in an invalid format. You don't pass a JSON string to that.
This parameter can either be passed as
a urlencoded string like
'para1=val1¶2=val2&...' or as an
array with the field name as key and
field data as value.-- PHP docs for curl_setopt()
Update
The quick way to avoid the SSL error is to add this option:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
You get an SSL verification error because cURL, unlike browsers, does not have a preloaded list of trusted certificate authorities (CAs), so no SSL certificates are trusted by default. The quick solution is to just accept certificates without verification by using the line above. The better solution is to manually add only the certificate(s) or CA(s) you want to accept. See this article on cURL and SSL for more information.
what would cause curl to return false when trying to access a local file?
After your curl execution, put something like this :
echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
You'll see what failed during your curl execution.
More info : curl_getinfo curl_errno curl_error
PHP Curl Get Request Returning False
You need to urlencode()
only the value parts of your query string, fields Postcode
and Address1
in this case.
<?php
$ch = curl_init();
//var_dump($url.$postData);
$requestUrl = "https://cleanlead.dlg.co.uk/?";
$requestUrl .= "circuit=public&fuseaction=lead&Campid=599&Suppid=571&Surveyid=5&Title=Mr&Forename=Adrian&Surname=Reut";
$requestUrl .= "&Address1=" . urlencode("8 Church Road Middlesex");
$requestUrl .= "&Finance_Insurance_PMI_CurrentlyHave=Yes";
$requestUrl .= "&Postcode=" . urlencode("UB3 2LH");
$requestUrl .= "&Finance_PMI_ThruCompany=No&TelephoneORMobile=2087976760&Age_Band=30-39";
curl_setopt($ch,CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$output=curl_exec($ch);
if (curl_error($ch)) {
var_dump();
}
curl_close($ch);
var_dump($output);
?>
Related Topics
Round Up to Nearest Multiple of Five in PHP
PHP Get Values from Json Encode
Array_Splice() For Associative Arrays
Notice: Array to String Conversion In
PHPmailer - Ssl3_Get_Server_Certificate:Certificate Verify Failed
Why Are $_Post Variables Getting Escaped in PHP
PHP Array Merge Two Arrays on Same Key
How to Successfully Rewrite Old MySQL-PHP Code With Deprecated MySQL_* Functions
Including PHP File from Another Server With PHP
PHP: How to Read a .Txt File from Ftp Server into a Variable
PHP Password_Hash(), Password_Verify()
PHP Getting Domain Name from Subdomain
Find Out How PHP Is Running on Server (Cgi or Fastcgi or Mod_PHP)
Json_Decode Returns Json_Error_Syntax But Online Formatter Says the Json Is Ok
PHP Redirection With Post Parameters