Curl sending twice each request - php

I have the follow curl command.
It takes 7 seconds to run on my local server, I found too long, I see the server log and realized that the curl is sending twice each request
$url = "http://192.168.0.106:8080/v1/devices/$deviceID/d0status/?access_token=$dispositivo";
// Initiate curl
$ch2 = curl_init();
// Disable SSL verification
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch2, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);//envia informação
curl_setopt($ch, CURLOPT_POSTFIELDS, array('params' => 'l1,HIGH'));//desliga tomada
curl_setopt($ch2, CURLOPT_TIMEOUT_MS, 1000);//
$result2=curl_exec($ch2);
curl_exec($ch2);
// Closing
curl_close($ch2);

I found the solution I have two curl_exec

Related

Need curl wait for response until the next execution

I sent 50-100 requests to my API at one time with curl, but I got this error :"This object cannot be accessed right now because another API request process is currently accessing it." So I really need to wait for the response of the recently request then execute the next request. How can I do that?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'site');
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('$header'));
curl_setopt($ch, CURLOPT_POSTFIELDS, '$postfield');
$result = curl_exec($ch);

proxy using curl and php

i am using with curl libary to scrape web site.
when i am using proxy nothing now shown.
i took the ip proxy from hide my ass list.
here is my code. someone can tell me what is wrong ?
$url = 'http://www.sherut24.co.il';
$proxy = '189.218.145.44:10000';
$ch = curl_init(); // Initialise a cURL handle
// Setting proxy option for cURL
// Set any other cURL options that are required
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$results = curl_exec($ch); // Execute a cURL request
curl_close($ch); // Closing the cURL handle
echo $results;

cURL doesn't wait for a response

I am trying to fetch data from an API with cURL for PHP.
When I execute following script, cURL doesn't seem to wait for the request.
It immediately returns the empty field, which couldn't be populated.
function request($cityName)
{
$key = "abc";
$api = "https://...?api-key=$key&format=json&city=$cityName";
$api = urldecode(trim($api));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, 'abc.com');
$ret = curl_exec($curl);
$response = json_decode($ret);
var_dump($response);
curl_close($curl);
}
Try this, don't use urldecode
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_USERAGENT, 'abc.com');
$server_output = curl_exec($ch);
curl_close($ch);
print_r($server_output);
The OAuth extension uses curl to make the request. By default CURL will generally verify the SSL certificate to see if its valid an issued by an accepted CA. To do this, curl uses a bundled set of CA certificates.
You can either disable the SSL checks ($oauth->disableSSLChecks()). Or ensure that you have a current version of curl. More information on curl certification verification.
There are chances that the cURL cancels the request because of low speed connetction.
This happens if the average transfer rate drops below CURLOPT_LOW_SPEED_LIMIT bytes/second for CURLOPT_LOW_SPEED_TIME seconds.
Try the following:
curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 1); // cancel cURL if below 1 byte/second
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 30); // Wait for 30 seconds
Change the values of the above options according to your requirements.

unable to execute an url through curl

I am unable to execute an URL through curl. But if simply run the query thrugh browser, its working fine. My code is like:
$xmlData = "<Leads><row no='1'><FL val='First Name'>".$new_fname."</FL><FL val='Last Name'>".$new_lname."</FL><FL val='Email'>".$new_email."</FL><FL val='Phone'>".$new_ph_no."</FL></row></Leads>";
$xmlData = htmlentities($xmlData);
$ch = curl_init("https://crm.zoho.com/crm/private/xml/Leads/insertRecords?newFormat=1&authtoken=XXX&scope=crmapi&xmlData=".$xmlData);
curl_setopt($ch, CURLOPT_VERBOSE, 1);//standard i/o streams
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);// Turn off the server and peer verification
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'rsa_rc4_128_sha'); //for godaddy server patch
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response)
curl_setopt($ch, CURLOPT_POST, 1);//Regular post
$response = curl_exec($ch);
curl_close($ch);
could you please let me know where is the issue?
Thanks in advance.
The following code should work:
$ch = curl_init("https://crm.zoho.com/crm/private/xml/Leads/insertRecords?newFormat=1&authtoken=XXX&scope=crmapi&xmlData=".$xmlData);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// deactivate certificate checking
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//Set to return data to string ($response)
curl_setopt($ch, CURLOPT_POST, 1);//Regular post
$response = curl_exec($ch);
if(!$response) {
echo curl_error($ch), PHP_EOL;
}
curl_close($ch);
After discussion comments I realized that the problem is, that you haven't installed the Thawte ssl certificate on your system. You have two options:
Install the certificates
Disable certificate checking using CURLOPT_VERIFYPEER = FALSE
I've used the second approach in my example just to show how to make your code working. For a production system I would advice you to install the certificates.

php + curl cannot set post method

I try to make a post request with php and curl. Here is my code
//PHP 5.3.5 and curl: 7.18.2
$ch = curl_init();
if(!empty($save_cookie)){
curl_setopt($ch, CURLOPT_COOKIEJAR, $save_cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $save_cookie);
}else{
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/post.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, !$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}
curl_close($ch);
return $postResult;
In http://localhost/post.php, I write
print_r($_SERVER);
The result return of curl is always
[REQUEST_METHOD] => GET
Remove the CURLOPT_NOBODY option and it will work. Or place it above the CURLOPT_POST line.
I think I have encountered this once, when trying to get just the header of a response. Setting
curl_setopt($ch, CURLOPT_NOBODY, true);
effectively instructs curl to issue a HEAD request, which is not a POST request. I think there is no way to just get the header from a POST (and just drop the connection after receiving the header). As a side effect, setting CURLOPT_NOBODY to false sets the request type to GET...
Do you really need the CURLOPT_NOBODY flag?
Try to move the
curl_setopt($ch, CURLOPT_NOBODY, !$body);
line right before the
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
line.
There's an interesting post at the curl/set_opt page, shedding some light on this behaviour:
If your POST data seems to be disappearing (POST data empty, request
is being handled by the server as a GET), try rearranging the order of
CURLOPT_POSTFIELDS setting with CURLOPT_NOBODY. CURLOPT_POSTFIELDS has
to come AFTER CURLOPT_NOBODY setting because if it comes after it
wipes out the header that tells your URL target that the
request is a POST not a GET.

Categories