Need curl wait for response until the next execution - php

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);

Related

Is this the correct way to use CURL (via PHP) to issue requests with a proxy?

Is this the correct way to use CURL (via PHP) to issue requests with a proxy?
$url ="https://www.website.com";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
curl_setopt($ch, CURLOPT_PROXY, "Proxy:Port");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response = curl_exec($ch);

PayPal Rest API PHP cURL login/auth not working anymore

We set up a PayPal express checkout about 2 weeks ago which worked perfectly until a couple of days ago it didn't.
On PayPal the transactions is ok but when I try to log in with cURL to check the transaction the inital merchant/client login fails but cURL shows no error.
I know that PayPal is upgrading to TLS 1.2 and HTTP/1.1 but I did the test check were our server/site passed.
This is how I initated the communication with paypal (this worked until now):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'CLIENT_ID:SECRET');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$result = curl_exec($ch);
Now if I print out $result its empty, if I print out curl_errno its 0 and curl_error also empty
so because of TLS 1.2 I modified the code like this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'CLIENT_ID:SECRET');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$result = curl_exec($ch);
but it fails exactly the same as the previous code.
What could be the problem?
or is it possible that if the login is ok the result is empty?

bad url never ending request curl php

I have a curl set up like this:
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
return $data;
}
When I call it with the following:
$html = file_get_contents_curl("https://training.sap.com/service-id/module.php/core/as_login.php?AuthId=sf-sp&ReturnTo=%2Fsuccessfactors-community%2Fpermission-check");
You can see that link has an authentication, and I do not want to spend time on it. I just want that curl session to die after maybe 3 seconds or x seconds, or determine before hand if it is going to be a never ending loop on the curl for one reason such a SSL or password requirement etc.

curl_multi_exec does not call remote function

I have a Utility class that has to invoke a remote cPanel URL to create an email forwarding address, via cURL. This works perfectly well if I use regular curl_exec but fails if I try to make it non-blocking via curl_multi_exec - All I need to do is send a request and move on, I dont need to know the result - so it is a truly non-blocking requirement
Here is the code that works
$url = "https://mycpanel.com:2083/execute/Email/add_forwarder?domain=mydomain.com&email=email_fwd_146&fwdopt=pipe&pipefwd=php/piper.php&";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode("user:pwd")));
$response = curl_exec($ch);
CakeLog::write('error', print_r($response, true));
curl_close($ch);
The response I get for above code is below (even though I really dont care about the response)
{
"messages":null,
"errors":null,
"status":1,
"metadata":{
"transformed":1
},
"data":[
{
"email":"email_fwd_146#mydomain.com",
"domain":"mydomain.com",
"forward":"|/home/myconsole/php/piper.php"
}]
}
Here is the code that does not work
$url = "https://mycpanel.com:2083/execute/Email/add_forwarder?domain=mydomain.com&email=email_fwd_146&fwdopt=pipe&pipefwd=php/piper.php&";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode("user:pwd")));
$mh = curl_multi_init();
curl_multi_add_handle($mh,$ch);
curl_multi_exec($mh, $active);
This call never seems to reach the server or even execute. Any insight will be appreciated.
This is because curl_multi_exec() needs to be called over and over again until the transfers are complete.

opening a page after another with curl

I want to open a page from a site and save the cookies. Then open another page with those cookies.(It doesn't let me to visit the second page without the first page's cookies)
I tried this code but it didn't work.
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cook.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cook.txt');
curl_setopt($ch, CURLOPT_URL, 'url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_exec($ch);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'nexturl');
$rs = curl_exec($ch);
echo $rs;
End of your each curl operation close the curl handle. Otherwise it will not save the cookie into the file.
$rs = curl_exec($ch);
curl_close($ch);
Now initiate second curl, and send the request. It should work now.

Categories