phpcurl losing connection after many refresh - php

am using this code to get access to myOwncloud server and it works fine but if i refresh the page i get this error "The requested URL returned error: 401 "
and if i wait 5 min or something it works again
any idea why?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url.($this->path ? '/'.$this->path : ''));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); /// 1
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
//PROPFIND request that lists all requested properties.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PROPFIND');
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Required for HTTP error codes to be reported via our call to curl_error($ch)
$response = curl_exec($ch);
if (false === $response) {
$response = curl_error($ch);
}
curl_close($ch);

Related

cURL error "Could not resolve host" when pointing towards local drive with XAMPP

In a certain API script, I want to run an AJAX file from within a PHP file, to post the vars to the AJAX file. I'd prefer not having to include the AJAX file for a nicer solution, since it's made to receive post vars. When I run the cURL locally using XAMPP I get the following error:
Curl error: Could not resolve host: C
Is semicolon an invalid char for cURL? How can I run cURL locally, pointing towards a file on the same server?
$isLocal = (!empty($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'], 'localhost') !== false);
if ($isLocal) {
$documentRootWildcard = 'C:/_foretag/TourTask/Internet/Hemsidor/example.com';
} else {
$documentRootWildcard = '/home/user5/_domains/example.com';
}
$curlUrl = $documentRootWildcard . '/ajax/bookings/save.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'ea5e7f894cde433a987d9b9979dfed5f:7ff527e78ace4e9291ee6f7a7d52141e');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json')
);
$curlResponse = curl_exec($ch);
if ($curlResponse === false) {
echo 'Curl error: ' . curl_error($ch);
} else { //Successfully sent
}
curl_close($ch);
How can I solve this? Or is there a better solution? Thank you!

Converge API - check if API is available

I am using Converge API to transfer funds from Credit Cards to my merchant account in my system.
This is the API URL that I am using:
https://api.convergepay.com/VirtualMerchant/process.do
Sometimes I am having connectivity issues to the API and as a result, I am getting blank screens or similar errors.
Is there a way I can check if the API is available before I do the CCSALE transaction? This is a simple code I setup to try and achieve this task.
$url = 'https://api.convergepay.com/VirtualMerchant/process.do';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
//$result will be false if the CURL request fails.
curl_close($ch);
Try getting the error info and then redirect the user. Use the code below to get a detailed response from CURL
$url = 'https://api.convergepay.com/VirtualMerchant/process.do';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_VERBOSE, true);
//Tell cURL that it should only spend 10 seconds
//trying to connect to the URL.
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
//A given cURL operation should only take
//30 seconds max.
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
$httpCode = curl_getinfo($curl , CURLINFO_HTTP_CODE);
$response = curl_exec($curl);
if ($response === false)
$response = curl_error($curl);
echo stripslashes($response);
curl_close($curl);

PHP - file get contents using CURL [duplicate]

This question already has answers here:
PHP CURL & HTTPS
(4 answers)
Closed 4 years ago.
i have a problem with curl, i trying to get content using CURL but return just null, i dont know what i miss, please tell me, here is my code :
$url = "https://shopee.co.id/api/v1/search_items/?by=pop&order=desc&keyword=yi 067&newest=0&limit=50&page_type=shop&match_id=16775174";
$html = file_get_contents_curl($url);
var_dump($html);
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST , "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'));
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
With curl, it's often a good idea to use curl_getinfo to obtain additional information about your curl connection before closing it. In your case, the NULL/FALSE/empty result could be due to a number of reasons and inspecting the curl info might help you find more detail. Here's a modified version of your function that uses this function to obtain additional information. You might consider writing print_r($info, TRUE) to a log file or something. It might be empty because the server response is empty. It might be false because the url cannot be reached from your server due to firewall issues. It might be returning an http_code that is 404 NOT FOUND or 5XX.
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST , "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
if(curl_errno($ch)) {
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
if ($data === FALSE) {
throw new Exception("curl_exec returned FALSE. Info follows:\n" . print_r($info, TRUE));
}
return $data;
}
EDIT: I added curl_errno checking too.

PHP curl not working when url have .jpg and post fields

I am using php curl to ping and post data to a url. But it is not working.response of url is "not found" this comes when the posting url is "http://example.com/conversion_get/"
I suspect it was because of .jpg in curl url.
I am using same code in same application to post data. but it works fine. Can any body help me to sort out this issue?
My code:
$kps="6789345607655ABF5k23929ocern46789345607655ABF5k23929ocern4";
$url="http://example.com/conversion_get/pixel.jpg";
$ch = curl_init();
if (!$ch){die("Couldn't initialize a cURL handle");}
$ret = curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "kp=".$kps);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlresponse = curl_exec($ch);
curl_setopt($ch, CURLOPT_HEADER, 0);
if(curl_errno($ch))
echo 'curl error : '. curl_error($ch);
if (empty($ret)) {
curl_close($ch);
} else {
$info = curl_getinfo($ch);
curl_close($ch); // close cURL handler
$ns= $curlresponse;
}

Fiddler to capture external webserver in PHP curl

I need to capture the request headers and response while using PHP cURL to post data to external API call. The localhost page load shows in traffic where as the PHP cURL is not shown.
$url = "https://https://gds.eligibleapi.com/v1.3/enrollment.json";
$ch = curl_init(); // initialize curl handle
$user_agent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // add POST fields
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_string)));
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch); // if($data === false) echo 'Curl error: ' . curl_error($ch);
echo $data;
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Your URL is malformed (you have https://https://).
You need to set the Proxy option on the CURL command, e.g.
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
See http://fiddler2.com/documentation/Configure-Fiddler/Tasks/ConfigurePHPcURL
You have to configure Fiddler to decrypt HTTPS traffic.
See here http://fiddler2.com/documentation/Configure-Fiddler/Tasks/DecryptHTTPS

Categories