bad url never ending request curl php - 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.

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

curl request to facebook redirects me to https://facebook.com/common/invalid_request.php

This is a part of the code of a function of a class that I did in early 2015. Today it is no longer working. The variables defined in my code, so that is not the problem...
Now that we're clear, let's get to the point:
The Problems:
Do not generate cookies
In the login page , I get a red sign with "
security policy " ...
I get redirected to
https://facebook.com/common/invalid_request.php when I 'm logged (
below I explain as I did for logging )
Poorly made solutions :
To generate cookies , all you did was use an extension for Chrome that shows you cookies.txt called cookies in Netscape format and copy the file with the facebook and log in because I did not allow log in with cookies without Log .
I could not fix it
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->CONFIG['cookie']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $this->CONFIG['useragent']);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.facebook.com');
curl_exec($ch) or die(curl_error($ch));
curl_close($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($email).'&pass='.urlencode($pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->CONFIG['cookie']);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->CONFIG['cookie']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $this->CONFIG['useragent']);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.facebook.com');
$page = curl_exec($ch) or die(curl_error($ch));
file_put_contents(getcwd().'/debug.html', $page);
preg_match('/<body class="([^<]+)" dir="ltr">/', $page, $web);
if(file_exists($this->CONFIG['token'])) unlink($this->CONFIG['token']);
if(preg_match('/home/i', $web[1])) {
$this->login = true;
return true;
} else {
return false;
}
curl_close($ch);
You can´t login with CURL, you need to implement a proper login process and you NEVER need the password for this: https://developers.facebook.com/docs/facebook-login
I suggest using the JavaScript SDK for this, it´s very easy to handle. Here are some links to get you started:
https://developers.facebook.com/docs/facebook-login/web
http://www.devils-heaven.com/facebook-javascript-sdk-login/

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.

How do I call an API without reloading the page with PHP?

I am looking to call the Twitter API to grab tweets (successfully achieved tweets on load) but now I am looking to update the page automatically, allowing the tweets to automatically load without reload/user interaction.
I know this type of functionality is possible (monitter.com) does it, but what is the technology used to do so? Can I do it with PHP?
Thanks
As #suresh.g said, you can use AJAX. The simplest way: use jQuery.
Also, you can use an iframe that reloads every 10 seconds with the setInterval() javascript function. The user will not have a reload of his entire page, but the twitter iframe.
Another type of technology is COMET or PUSH technology, but I don't think you need it right now, but it's good to know about it ;)
use curl
function curl_grab_page($url,$data,$secure="false",$ref_url="",$login = "false",$proxy = "null",$proxystatus = "false")
{
if($login == 'true') {
$fp = fopen("cookie.txt", "w");
fclose($fp);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if ($proxystatus == 'true') {
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if($secure=='true')
{
curl_setopt($ch, CURLOPT_SSLVERSION,3);
}
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start();
return curl_exec ($ch); // execute the curl command
curl_getinfo($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
}
just call this function the way you want the data you want to set it can do every thing for you just dont forget to setup curl in you php.ini
thanks

using CURL and avoiding being redirected to the base url

Im trying to get the source code of a page with CURL
http://www.deindeal.ch/zurich/?a_aid=dealanzeiger&a_bid=62e0def7
but the only thing I get is the index site (http://www.deindeal.ch), as I would be redirected automatically. I suppose they are avoiding hotlinking or something like that? And in that case, how could I get the source code, maybe some curl setopt ?
My CURL connection:
$ch = curl_init();
$timeout = 60;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_HTTPGET, false);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
Thanks!
Take out this option:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
However, if the site's redirecting, they may not output anything EXCEPT the redirect, so not following the redirect still won't get you any content, because they just flat-out don't send it.

Categories