cURL development load slowly after uploaded in server PHP - php

Hi "I would like to ask an assistance" yes, you read it right, I don't know what's happening to my cURL development program. Straight to the point, I have cURL development program which works perfect and faster in my localhost xampp, I even receives a response correctly. But I thought i'm done on my work, until I uploaded it to my cpanel, It became superslow in terms loading, It cannot receives any response anymore also after loading.
I just receive
Curl Error: 7
OR
Curl Error: 28
I don't know whats this weird thing. I googled already the errors (cURL error(7) "It says connection issue to host, but its impossible, I can connect to host in my localhost." cURL error: 28 I have made some adjustment also to my curl_opt() you see in my comment below.
Here's my code
$headers = array(
// "Accept-Encoding: gzip, deflate",
"Content-Type: text/xml;charset=\"UTF-8\"",
"SOAPAction: \"http://domain.org/\"",
"Host: domain.com",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
// PHP cURL for https connection with auth
$soap_do = curl_init() or die('Error');
set_time_limit(0);
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 400);
curl_setopt($soap_do, CURLOPT_AUTOREFERER, true); // try, to solve an issue
curl_setopt($soap_do, CURLOPT_MAXREDIRS, 10); // try, to solve an issue
curl_setopt($soap_do, CURLOPT_FOLLOWLOCATION, true); // try, to solve an issue
curl_setopt($soap_do, CURLOPT_TIMEOUT_MS, 400); // try, to solve an issue
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($soap_do, CURLOPT_VERBOSE, TRUE);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
But still the same, No response coming and loads slow

Whenever I run into problems with cURL, I place the curl handle that has just completed to PHP's curl_getinfo function.
Try adding this code just after you run curl_exec($soap_do);
echo '<pre>'; print_r(curl_getinfo($soap_do)); echo '</pre>';
curl_getinfo returns an array of data, which is displayed by print_r, and formatted in an easy to read list by the <pre> tag.

Related

cURL makes page load forever, then gateway timed out

I'm working with a third party API, and it returns 400 HTTP STATUS when you make a mistake.
When using POSTMAN, the 400 HTTP error page shows as expected, but when using PHP's cURL, the page just loads for about a minute, then it gives me a "504 - Gateway timeout", and I need to close the browser to be able to access the page again.
When the cURL request is valid (the API returns a 200 OK) it works normally.
My question is, why is cURL giving the 504 error instead of 400?
And why is it ignoring CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT?
Below is my cURL code (both $token, $payload and $config variables are declared and valid):
$ch = curl_init("https://api.mercadolibre.com/items?access_token=$token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"User-Agent: Alcavie/" . $config['version'],
"Accept: application/json",
"Content-Type: application/json",
//"Accept-Encoding: gzip, deflate, br",
//"Connection: keep-alive"
));
$error = false;
try{
$result = curl_exec($ch);
$resultJson = json_decode($result);
}catch(\Exception $e){
var_dump($e);
$error = true;
exit;
}
curl_close($ch);
The API probably sends HTTP302 in between and if so, you'd either need two requests ortell cURL to follow the redirect with curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);HTTP504 means that the client didn't react in a timely manner upon the previous HTTP header;there may be other causes for HTTP504, but it is one of the most common. Postman is insufficient to test this properly (I don't use it at all); better use a web-browser, hit F12 and tick the "preserve logs" checkbox... then you can clearly see which HTTP headers are being sent in succession.
Also useful to debug this would be: curl_setopt($ch, CURLOPT_HEADER, true);

How can I access the Puppet API using PHP

I am trying to get a PHP script to access the Puppet API. I have spent 2 days searching and cannot believe that I cant find any info whatsoever (only puppet modules for installing PHP).
I am just trying to use PHP and curl but I am not able to get any kind of response, error or anything. Here is my (very basic) attempt to get the cert from the puppet master:
function get_data($url) {
$request_headers = array();
$request_headers[] = 'Accept: s';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$response = get_data('https://<puppet master>:8140/production/certificate/ca');
All I am trying to replicate is a curl call that works from my server:
curl -k -H "Accept: s" https://<puppet master>:8140/production/certificate/ca
I have a feeling that there is probably something obvious I am missing but, I cannot figure it out.
Thank to Wrikken and Glen for getting me on track. Once I set the curl_setopt options correctly, it works as expected.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/cacert.pem");

How to add the custom headers to the https request curl PHP script?

I am writing the following code
<?php
$ch=curl_init();
$h=array("Host:v.airtellive.com");
curl_setopt($ch,CURLOPT_URL,"https://facebook.com");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLINFO_HEADER_OUT,true);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$h);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow header redirections
curl_setopt($ch, CURLOPT_MAXREDIRS, 4); // Limit redirections to 4
$p=curl_getinfo($ch);
$o=curl_exec($ch);
if($o==null)
var_dump (curl_error($ch));
$headers=curl_getinfo($ch,CURLINFO_HEADER_OUT);
print_r($headers);
echo "<xmp>";
echo $o;
curl_close($ch);
?>
but it gives me error:
could not resolve the host
If I use this script with search.yahoo.com it works fine. Actually, I want to create the realhost proxy by sending the host headers.
Headers for CURL mus be set like this
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER , array(
"AUTH_USERNAME: {$username}",
"AUTH_PASSWORD: {$password}",
));
$curlReturn = curl_exec($curl_handle);
curl_close($curl_handle);
Any number of custom headers can be sent as array in this.

Translating cURL command to PHP

I'm trying to translate the following cURL command to PHP.
curl -k https://api.box.com/2.0/files/content -H "Authorization: Bearer 8tuuIMsrf3ShyODVdw" -F filename=#txt.txt -F folder_id=0
Here is my attempt:
$url = 'https://api.box.com/2.0/folders/content';
$params['filename'] = '#txt.txt';
$params['folder_id'] = '0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$this->access_token, "Content-Type: multipart/form"));
$data = curl_exec($ch);
curl_close($ch);
It's not working however. Can anyone help me understand why what I'm doing is wrong and what I can do to modify my code? Thanks!!
Try getting rid of the header "Content-Type: multipart/form" from your CURLOPT_HTTPHEADER option as cURL will take care of this for you based on your post data.
Since you are uploading a file, the content-type will automatically be set to multipart/form-data which is actually the correct content type, not multipart/form.
curl -k means insecure. Add the following line:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Translated from the german manual (Note that the english version is missing this information):
If CURLOPT_SSL_VERIFYPEER has been deactivated, the option CURLOPT_SSL_VERIFYHOST has to be disabled as well.
The rest of your code should work fine.

See what CURL sends from a PHP script

I'm having dificulties to query a webform using CURL with a PHP script. I suspect, that I'm sending something that the webserver does not like. In order to see what CURL realy sends I'd like to see the whole message that goes to the webserver.
How can I set-up CURL to give me the full output?
I did
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
but that onyl gives me a part of the header. The message content is not shown.
Thanks for all the answers! After all, they tell that It's not possible. I went down the road and got familiar with Wireshark. Not an easy task but definitely worth the effort.
Have you tried CURLINFO_HEADER_OUT?
Quoting the PHP manual for curl_getinfo:
CURLINFO_HEADER_OUT - The request string sent. For this to work, add
the CURLINFO_HEADER_OUT option to the handle by calling curl_setopt()
If you are wanting the content can't you just log it? I am doing something similar for my API calls
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::$apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, count($dataArray));
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$logger->info("Sending " . $dataString);
self::$results = curl_exec($ch);
curl_close($ch);
$decoded = json_decode(self::$results);
$logger->debug("Received " . serialize($decoded));
Or try
curl_setopt($ch, CURLOPT_STDERR, $fp);
I would recommend using curl_getinfo.
<?php
curl_exec($ch);
$info = curl_getinfo($ch);
if ( !empty($info) && is_array($info) {
print_r( $info );
} else {
throw new Exception('Curl Info is empty or not an array');
};
?>

Categories