so here is my problem ... I have a php-curl function that sends a POST Request to an API with a file as a parameter.
My Code is working perfectly fine on the localhost and its not working on my server.
Server Details
- Dedicated VPS windows server 2012
- Installed PHP 5.5 and MySQL 5.5 and phpmyadmin
- Curl is enabled ... && tried a simple curl function and its working fine
My app
- My Code is working perfectly fine on localhost but its not working on the server (i tried multiple servers with multiple specs)
$localFile=$_FILES['file_c']['tmp_name'];
$curl = curl_init($url);
if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
$data = array(
'version' => '2017-01-20',
'file' => new CURLFile($localFile),
'config'=> '{"conversion_target":"normalized_html"}'
);
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
}else{
$data = array(
'version' => '2017-01-20',
'file' => "#".$localFile,
'config'=> '{"conversion_target":"normalized_html"}'
);
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Insert the data
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
// Send the request
if(!curl_exec($curl)){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}else{ //do something else}
so this piece of code works fine on localhost but on the server it shoots a 500 internal server error
no errors in server logs
I've traced every single line in that code it breaks at the curl execution line
Please help
Thanks!
Related
$user_access ='example';
$user_key = 'examplekey';
$payload = json_encode($arr);
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'example.com/api/users/2');
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($payload)));
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $payload );
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERPWD, $user_access . ":" . $user_key);
if (curl_exec($curl_handle) === FALSE) {
die("Curl Failed: " . curl_error($curl_handle));
} else {
return curl_exec($curl_handle);
};
When I remove the curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); then it is working on both local as well as live server. I do not need the return automatically curl_exec value so that I am using curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
I unable to identify the issue. Please help me
If you checked that you use a likely environment (e.g. PHP-Version, curl_exec not blocked on live server which is the case for some hosters) locally and on your live server than it is most probably that the remote server (the API-Server) is in fact responding differently. This can be that it has access-levels based on ip-address or other things.
In general consider:
You are executing curl_exec() twice: First to check if it's return is FALSE and then again if it was not. Instead store the return value in a variable
Try setting curl_setopt($curl_handle, CURLOPT_VERBOSE, true); so you get more information about potential errors
Using a HTTP-Request Library that handles those things for you, like Guzzle wich brings also good error-handling. After installing it:
$httpClient = new GuzzleHttp\Client();
$response = $client->request('PUT', 'https://example.com/api/users/2', [
'auth' => [$user_access, $user_pass],
'json' => $arr
]);
return $response->getBody();
will handle everything for you.
You should be comparing and handling like below;
$Result = curl_exec($curl_handle);
if($Result === false)die("Curl Failed: " . curl_error($curl_handle)); // Just an error display interceptor
return $Result; // Always return the appropriate response
It is certain in your code that you are trying to wrap this in a function, and this function should always return something. Here we are returning the $Result no matter cURL succeeded or failed, you make an explicit check with the caller to see if your cURL method worked ($cURLFunctionReturnValue === false) and code like that.
I am using Php as a frontend and Java as a backend. I have created an Post API for uploading file and using curl for api request.
I have hit my Api using Postman at that time it works fine but i am facing prodblem when i request api using Curl i don't eble to get what i am doing wrong.
Here is the curl requested data :-
$data2 = array(
'file' =>
'#' . $data1->file->tmp_name
. ';filename=' . $data1->file->name
. ';type=' . $data1->file->type
);
This is how i am sending curl request:-
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch,CURLOPT_URL,$this->url);
curl_setopt($ch, CURLOPT_HEADER, 1); //parveen
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); //parveen
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data2);
$headers = array(
'Content-Type:'.$this->service->contentType,
'Launcher:'.$this->serverName,
'domain:'.$this->service->domain,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$this->responseBody=curl_exec($ch);
Links where i find this solution:-
enter link description here
I search a lot to find the solution but nothing is worked for me so please help me .
Thanks
the way you're trying to upload the file hasn't been supported since the PHP5 days, and even in 5.5+ you'd need CURLOPT_SAFE_UPLOAD to upload with #. use CURLFile when uploading files, like
$data2 = array(
'file' => new CURLFile($data1->file->name,$data1->file->type,$data1->file->tmp_name)
);
also, don't use CURLOPT_CUSTOMREQUEST for POST requests, just use CURLOPT_POST. (this is also true for GET requests and CURLOPT_HTTPGET )
also, check the return value of curl_setopt, if there was a problem setting your option, it returns bool(false), in which case you should use curl_error() to extract the error message. use something like
function ecurl_setopt($ch,int $option,$value){
if(!curl_setopt($ch,$option,$value)){
throw new \RuntimeException('curl_setopt failed! '.curl_error($ch));
}
}
and protip, whenever you're debugging curl code, use CURLOPT_VERBOSE, it prints lots of useful debugging info
I am trying to get facebook access token
But the url when executed from browser and local machine php script returns true data values. However, when i run the same script from server, it fails and shows bool(false)
the url
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=".$config['appId']."&redirect_uri=" . urlencode($config['callback_url'])
. "&client_secret=".$config['secret']."&code=" . $_GET['code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec ($ch);
var_dump($response);
Other urls work on the server.
What is wrong here ?
You should check at the curl error instead of only looking at the curl_exec result:
PHP 4 >= 4.0.3, PHP 5, PHP 7)
curl_error — Return a string containing the last error for the current session
Try this:
if(curl_exec($ch) === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
echo 'Operation completed without any errors';
}
curl_close($ch);
UPDATE
Try setting this:
'curl' => [ CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4]
For more details check here
Try to check curl_error
curl_error — Return a string containing the last error for the current session
That may help finding what was wrong .
I have been given to understand from an upvoted answer on one of the other questions that my server Hostinger (free) does not allow connecting to Facebook SDK !!!
I am moving to a paid server !
I am trying to connect to the Marketo.com REST API using curl.
I can't get a response from the identity service. I only get an error message
"[curl] 6: Couldn't resolve host 'MY_CLIENT_ENDPOINT.mktorest.com'
,
but I can print the constructed url and paste it into a browser address bar and this will provide the expected response with the access_token element.
I can use curl in php and in a terminal to access my gmail account so curl is able to access an https service.
I have tried sending the parameters in the curl url as a get request and also by declaring them with curl's -F option as a post request
My application uses dchesterton/marketo-rest-api available on github, but I have also tried a simple php curl request just to get the access token.
private function getToken() {
$url = "$this->client_url/identity/oauth/token?grant_type=client_credentials&client_id=$this->client_id&client_secret=$this->client_secret";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$errors = curl_error($ch);
curl_close($ch);
file_put_contents($this->logDir . 'access_token_response' . date('Y-m-d') . '.txt', $url . "\n" . $response . "\n", FILE_APPEND);
if ($errors) {
file_put_contents($this->logDir . 'access_token_errors' . date('Y-m-d') . '.txt', $errors . "\n", FILE_APPEND);
}
return $response['access_token'];
}
Again, this fails with the same error but produces a perfectly formed url that I can paste into the browser and get a valid response.
I have also tried this using post instead of get as I have for every other test mentioned, and these have been tried on my localhost and on a test server.
Can anyone explain to me why this would fail?
Does Marketo block curl on a per account basis?
I was trying to implement something similar but my code wasn't working. I'm not sure exactly what is failing but I tried your code and it seems to work perfectly after some slight modifications:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($request_data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($curl);
$errors = curl_error($curl);
curl_close($curl);
I hope this helps.
Hi I'm doing a website right now. Both of these files is in one server and domain and I'm using cloudflare to boost the loading. I'm using Full SSL option on cloudflare because I bought my own SSL Geotrust on my server. I already upgraded my curl on the server to 7.41.0.
One php file consist of the function
Function File:
<?php
function get_content($session){
$endpoint = "https://sample.ph/php/resource.php";
// Use one of the parameter configurations listed at the top of the post
$params = array(
"yel" => $session
);
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$endpoint);
$strCookie = 'PHPSESSID='.$_COOKIE['PHPSESSID'];
curl_setopt($curl, CURLOPT_COOKIE, $strCookie);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
$postData = "";
//This is needed to properly form post the credentials object
foreach($params as $k => $v)
{
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_HEADER, 0); // Don’t return the header, just the html
curl_setopt($curl, CURLOPT_CAINFO,"/home/sample/public_html/php/cacert.pem"); // Set the location of the CA-bundle
session_write_close();
$response = curl_exec($curl);
if ($response === FALSE) {
return "cURL Error: " . curl_error($curl);
}
else{
// evaluate for success response
return $response;
}
curl_close($curl);
}
?>
Resource File
<?php
session_start();
if(isset($_POST['yel'])){
$drcyt_key = dcrypt("{$_POST['yel']}");
if($drcyt_key == $_SESSION['token']){
echo "Success";
}
}
?>
How do you think will I fix this?
The SSL Verification error. Upon debugging sometimes I got cURL Error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Sometimes I got cURL Error: SSL peer certificate or SSH remote key was not OK
When I put curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); to FALSE, which is not a good idea; There comes a second problem for the SESSION COOKIE becoming blank on first load.
I HOPE YOU CAN HELP ME. THANK YOU.
This issue looks to be an outdated certificate bundle or outdated OpenSSL version on the server. You should both ensure you have the latest root certificates on your computer and also ensure that you have the latest versions of OpenSSL (including the PHP OpenSSL module).