I use php curl to get content from a php script in the same domain url. But I get curl_exec error. The curl error code is 28 or operation timed out. After days of debugging, I found that it works on non script page like htm, but not php, it also works if the url is a script on different domain. I have been debugging for days and found no solutions. Helps appreciated.
$url = 'http://...';
$agent = '';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$result = curl_exec ($ch);
print "<pre>\n";
print_r(curl_getinfo($ch));
// get error info echo "\n\ncURL error number:" .curl_errno($ch);
// print error info echo "\n\ncURL error:" . curl_error($ch);
print "</pre>\n";
curl_close ($ch);
echo $result;
cURL error number:28 cURL error:
Operation timed out after 8000
milliseconds with 0 bytes received
Okay: $url = http://.../page.htm
Fail: $url = http://.../page.php
You're not setting the User Agent. Some servers actually do not even respond to such requests (thus the client doesn't know the connection dropped).
Add the following to your code:
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
Additionally, I use the following in my CURL functionality:
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch,CURLOPT_MAXREDIRS,50);
if(substr($url,0,8)=='https://'){
// The following ensures SSL always works. A little detail:
// SSL does two things at once:
// 1. it encrypts communication
// 2. it ensures the target party is who it claims to be.
// In short, if the following code is allowed, CURL won't check if the
// certificate is known and valid, however, it still encrypts communication.
curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
Maybe because of session locks
Try to drop session_start() from page which you try to get using cURL
Also see this session.auto-start
Set into CURL setting
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 300 seconds
If still have issue so run the script with following location disable.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
Related
I am trying to use curl and php to send a file from my liquid web server to the bullhorn vms system. I have tried multiple ways to get this to work but the current code i am using is:
$url_path_str = 'https://rest91.bullhornstaffing.com/rest-services/(corp token)/file/Candidate/'.$entityid.'/raw?filetype=SAMPLE&externalID=portfolio?BhRestToken='.$bhresttoken;
$file_path_str = '/home/path/'.$row["first_name"].$row["last_name"].'.pdf';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
echo $url_path_str;
echo $curl_response_res;
curl_close($ch);
fclose($fh_res);
The $entityid, $bhresttoken, $row["first_name"], and $row["last_name"] are variables I have created and using echo shows that they are working. Two errors show up separately when I try to execute this code.
The first error is
"{"errorMessage":"Missing 'BhRestToken' in request header or parameter.","errorMessageKey":"errors.authentication.missingRestToken","errorCode":412}"
which is weird because the bhresttoken allegedly isn't required but also it is definitely showing correctly at the end of the "url_path_str", so is it in the wrong spot?
The other issue that I get is
"An error occurred while processing your request. Reference #179.24343217.1582146766.1f0b67".
From what I've been told the reference number is a generic issue so I'm guessing that my code is flawed somewhere however I cannot figure out what is wrong.
Any advice or input would be much appreciated. Thank you in advance.
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 have a function make_curl_request to make curl request.
/**
* General Function to make curl request */
function make_curl_request($url, $data)
{
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
curl_close($ch);
$strCurlResponse = ob_get_contents();
ob_end_clean();
return $strCurlResponse;
}
I am calling it like:
$strGatewayResponse = make_curl_request( REQUEST_URL, compact('strMobileNo', 'strKeywords', 'strApiKey') );
I tried the things but can't get my code working fine. Currently its just return string("") as the output. Where am i going wrong?
My target is to simple post few data to next page located on other domain and get its xml response and parse it and display it. Is there any other simple and good solution?
The problem is that you've got RETURNTRANSFER set to TRUE, which means curl returns its output instead of directly outputting it. However, you're not capturing that output in a variable, so it's dropping on the floor.
You've got two options
a) remove the ob_*() functions to remove the PHP buffering and then do
$data = curl_exec($ch)
if ($data === FALSE) {
die("Curl failed: " . curL_error($ch));
}
after which $data contains the contents of the URL you've fetched.
b) remove the RETURNTRANSFER option, and let curl do its normal "output to client directly" thing, which then gets captured by the PHP output buffering.
Try by adding a row:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.
it's not ok to send a curl POST without a header.
please find the following link. it may help
OAuth, PHP, Rest API and curl gives 400 Bad Request
I know how to set the timeout in cURL but I want to alert the user that the request timed out.
I have created an ajax script that allows the user to request data from various insurance sites and aggregate into a list. If any of the insurance sites fail to respond within a certain time I want to alert the user that the current quote from that company is not available at the moment.
Does cURL return anything to signal a timeout?
curl_errno() returns 28 if the operation timed out. See http://curl.haxx.se/libcurl/c/libcurl-errors.html for other error codes.
Or another solution that can cover even more cases (server timed out, server errored out with a blank page) is to check if your get_url function result is different that "" or FALSE.
Example of get_url function :
function get_url($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$tmp = curl_exec($ch);
curl_close($ch);
return $tmp;
}
I have two project on same server. I want some data form on my website so I am using file_get_contents; most of the time I get the 500 internal error
I checked that my url fopen is on using phpinfo().
With default settings, file_get_content() doesn't work behind a proxy or it cannot handle timeouts. It's normally recommended to read local files.
Therefore use cURL instead.
Below function could be used for the job:
function http_request($uri, $time_out = 10, $headers = 0)
{
// Initializing
$ch = curl_init();
// Set URI
curl_setopt($ch, CURLOPT_URL, trim($uri));
curl_setopt($ch, CURLOPT_HEADER, $headers);
// 1 - if output is not needed on the browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Time-out in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
// Executing
$result = curl_exec($ch);
// Closing the channel
curl_close($ch);
return $result;
}
Let me know whether your're using Linux or Windows to give you cURL installation tips