How to use "if statement" in curl - php

My question is, I'm using curl to get spesific url from a site, but the site is very hude and loads slow, the part where i'm going to fetch is on the top of the page so the part i need loads fast. How to stop load the curl if my if statement is happened?
I tried =
$con = curl_init();
curl_setopt($con, CURLOPT_URL, $url_get);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($con, CURLOPT_POST, true);
curl_setopt($con, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)");
curl_setopt($con, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($con, CURLOPT_COOKIEFILE, $cookie_path);
curl_setopt($con, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($con, CURLOPT_COOKIESESSION, true);
$veri = curl_exec($con);
if(preg_match("/value='(.*?)[+++](.*?)href='"$url"' style/", $veri, $dl)){
curl_close($con);
so if I get the $url in the page, it has to stop curl. Is it possible? Or any other ways to do it.
The value I need is $dl[1];

Related

Site not loading using curl

<?php
$url='https://source.amazon.com/forcelogin?returnToURL=https://www.amazon.com/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
curl_setopt($ch, CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
$data = curl_exec($ch);
curl_close($ch);
?>
I don't know why it doesn't load the website.
If I put regular sites like google.com, amazon.com or anything else is working.Anyone can help me out?
You need to add CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to access HTTPS URL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl post php 403 forbidden

i'm tring to post my data using POST methode and curl excute , but the result is
403 - Forbidden: Access is denied. You do not have permission to
view this directory or page using the credentials that you supplied.
$request='';
$url='https://app.highwire.com/api/auth';
$header=array('mydata');
$con = curl_init();
curl_setopt($con, CURLOPT_URL, $url);
curl_setopt($con, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($con, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($con, CURLOPT_HEADER, true);
curl_setopt($con, CURLOPT_HTTPHEADER, $header);
curl_setopt($con, CURLOPT_POST, 1);
curl_setopt($con, CURLOPT_POSTFIELDS, $request);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
curl_setopt($con, CURLOPT_FAILONERROR, 0);
curl_setopt($con, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
$res = curl_exec($con);
curl_close($con);

CURL: PHP : can't submit

I need to write a php script that will login to my admin page then submit rss.
I'm able to login with the code below, but can't submit the rss
<?php
function rssadd($url,$post,$post2) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.4) Gecko/2008102920 AdCentriaIM/1.7 Firefox/3.0.4');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$ch2 = curl_init($url);
curl_setopt($ch2, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.4) Gecko/2008102920 AdCentriaIM/1.7 Firefox/3.0.4');
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $post2);
curl_setopt($ch2, CURLOPT_REFERER, $url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$result2 = curl_exec($ch2);
return $result . $result2;
}
$page2 = rssadd('http://site.com/admin.php?mod=rss&action=news&id=4','subaction=dologin&username=admin&password=pass','subaction=doit');
echo $page2;
?>
the html on "http://site.com/admin.php?mod=rss&action=news&id=4" i'm not able to submit
<input type="submit" name="subaction" value="doit" class="buttons">
Perhaps you need to retain cookies between requests? Try setting CURLOPT_COOKIEFILE to '' (the empty string). You'll also need to use the same curl handle on both requests - instead of closing the first handle and initializing a new one, just change the options on the first one and run it again.
I originally learned how to do this from this Stack Overflow answer: https://stackoverflow.com/a/5758471/638544

Can't get user_id who share post in facebook using FQL Query or CURL

I try using FQL Query to fetch count of share from post on facebook page but I don't know table who provide this. So my final decision is using CURL to get data.
I'm using CURL and get 2 persons who share that post actually the data is 27 persons. Ajax function on page it's not reload data again and result keep 2 persons.
If Ajax Function on page works correctly, I can fetch all users who share post.
Another method using FQL Query or Facebook SDK really appreciate..
Could I fetch all users who share post on facebook ?
Thanks in Advance.
$login_email = 'myemail';
$login_pass = 'mypassword';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_exec($ch);
$url= 'http://www.facebook.com/shares/view?id=390010811012261';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.facebook.com');
curl_setopt($ch, CURLOPT_URL, $url);
$curl_dt= curl_exec($ch);
Scraping Facebook pages like this is a violation of it's site policies (3.2 of the SRR)
Please file a feature request in the Bug tracker to get the full list of Shares in the API for Post objects (currently we only serve the number of shares, not the full info about them)

How to store multiple cookies through PHP Curl

'SOUP.IO' is not providing any api. So Iam trying to use 'PHP Curl' to login and submit data through PHP.
Iam able to login the website successfully(through cUrl), but when I try to submit data through cUrl, it gives me error of 'invalid user'.
When I tried to analysed the code and website, I came to know that cUrl is getting values of only 1-2 cookies. Where as when I open the same page in FireFox, it shows me 6-7 cookies related to 'SOUP.IO'.
Can some one guide me how to get all these 7 cookies values.
Following cookies are getable by cUrl:
soup_session_id
Following cookies are shown in Firefox (not through cUrl):
__qca, __utma, __utmb, __utmc, __utmz
Following is my cUrl code:
<?php
session_start();
$cookie_file_path = getcwd()."/cookie/cookie.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.soup.io');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) FirePHP/0.4');
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>
Can some one guide me in this regards
Thanks in advance
These extra "underscore" cookies seem like Google Analytics or similar tracking cookies, most likely set via Javascript. That's the reason they don't show up when using cURL. I'd venture the guess that they're not the problem.
A couple of things i noticed once i signup and went into the area. The domain all my action happens is "user.soup.io" and not "www.soup.io" , that could be the reason behind your invalid user error. Try to set the url to your own subdomain AFTER the login is complete and see how it goes. Also what data are you exactly trying to post?
This may not be relevant but soup.io doesnt seem to use HTTPS, so why use:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
Here is my curl code which Iam trying to use for sending data to soup.io after suceesful login through cUrl
$storedata = array();
$storedata["post[title]"] = 'Phoonk 2 (16th April 2010)';
$storedata["post[body]"] = 'Ramgopal Varma\'s love for horror and supernatural continues. This time, in PHOONK 2, the team behind PHOONK promise more chills, more thrills and more screams. But what you get to hear at the end of the screening is a moan, since PHOONK 2 lacks the chills, thrills and screams that were the mainstay of its first part.';
$storedata["post[tags]"] = 'Bollywood Movie, Indian movie';
$storedata["commit"] = 'Save';
$storedata["post[id]"] = '';
$storedata["post[type]"] = 'PostRegular';
$storedata["post[parent_id]"] = '';
$storedata["post[original_id]"] = '';
$storedata["post[edited_after_repost]"] = '';
$store_post_str = '';
foreach($storedata as $key => $value){
$store_post_str .= $key.'='.urlencode($value).'&';
}
$store_post_str = substr($store_post_str, 0, -1);
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, 'http://loekit.soup.io/save');
curl_setopt($ch2, CURLOPT_VERBOSE, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch2, CURLOPT_HEADER, TRUE);
curl_setopt($ch2, CURLOPT_ENCODING, 'gzip,deflate');
//curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie_file_path);
//curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch2, CURLOPT_REFERER, 'http://loekit.soup.io/');
curl_setopt($ch2, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) FirePHP/0.4');
curl_setopt($ch2, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $store_post_str);
curl_setopt($ch2, CURLOPT_POST, TRUE);

Categories