Curl not giving any response back - php

I am using curl get the response back from a file on different server..
Curl code:
$url="http://example.com/trck.php?adrs=yy";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
trck.php Code:
$url=mysqli_real_escape_string($link,$_GET['adrs']);
$sql="SELECT * FROM product where site = '$url'";
$res=mysqli_query($link,$sql) or die( mysqli_error() );
$row=mysqli_fetch_assoc($res);
if(mysqli_num_rows($res)==0 || $row['status']==0)
{
return no;
}
else
{
return yes;
}
but there is no response form it.
Help will be appreciated.

$url=mysqli_real_escape_string($link,$_GET['adrs']);
$sql="SELECT * FROM product where site = '$url'";
$res=mysqli_query($link,$sql) or die( mysqli_error() );
$row=mysqli_fetch_assoc($res);
if(mysqli_num_rows($res)==0 || $row['status']==0)
{
echo 'no';
}
else
{
echo 'yes';
}
also:
$url="http://example.com/trck.php?adrs=yy";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
you have to echo the returned transfer from curl.

Related

How to check entered url is indexing on google or not using php

I am trying to create a script that should check all indexed pages from Google. But it will not give proper output.
<?php
function indexed($url) {
$url = 'http://webcache.googleusercontent.com/search?q=cache:' . urlencode($url);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Chrome 10');
if (!curl_exec($ch)) {
// var_dump('failed');
return false;
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//var_dump($code);
return $code == '200';
}
echo indexed('http://www.berlin-info.de/de/tourist-info/urlaub-in-kanada');
?>

Basic Authorization PHP with Server related

I have some problem that related to HTTP_HEADERS in curl php in opencart. The code below is caller.
$ch = curl_init();
$url = 'http://aaa.com/index.php?route=common/home/getTotalCustomer';
$url2 = 'http://bbb.com/index.php?route=common/home/getTotalCustomer';
$url3 = 'http://ccc.com/index.php?route=common/home/getTotalCustomer';
$header = array('Authorization:Basic ' . base64_encode('user:password'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$results = curl_exec($ch);
echo '<pre>';
print_r(json_decode($results, true));
echo '</pre>';
The receiver code like below:
public function getTotalCustomer(){
$json = array();
$this->load->model('account/customer');
if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){
if(($_SERVER['PHP_AUTH_PW'] == 'password') && ($_SERVER['PHP_AUTH_USER'] == 'user')){
$json['total_customer'] = $this->model_account_customer->getTotalCustomer();
}
} else{
$json['message'] = 'failed';
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
I had tried in multiple domain with different servers. Some server can return the data but some server cannot return the data. Why?
Your header that you're sending is incorrect.
You're passing
Authorization:Basic <username:password>
it should be
Authorization: Basic <username:password>
note the space.

Terminate function a() when function b() failed

We are a little bit confuse how we can achieve this problem. We don't want to run the createsite function in our code if createSubaccount function fails. We would truly appreciate any feedbacks, comments, guides on our code.
<?php
//Set API user and password
define("API_USER","user");
define("API_PASS","pw");
$createdSite = createSite($_REQUEST['template_id'],$_REQUEST['original_url']);
//echo 'Site Created: ' . $createdSite . '<br/>';
$accountCreated = createSubAccount($_REQUEST['email']);//client email
//echo 'Account created: ' . $accountCreated . '<br/>';
$first_name = $_REQUEST['first_name'];//First Name
$last_name = $_REQUEST['last_name'];//Last Name
$retArr = ["sso"=>$sso_link,"ru"=>$resetURL,"ac"=>$accountCreated,"fn"=>$first_name,"ln"=>$last_name];//assoc array
print json_encode ($retArr);//json string
function createSite($template_id,$original_url) {
//create array with data
if($original_url) {
$data = array("template_id"=>$_REQUEST['template_id'],"url"=>$original_url);
} else {
$data = array("template_id"=>$_REQUEST['template_id']);
}
//turn data into json to pass via cURL
$data = json_encode($data);
//Set cURL parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/api/create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template data
$output = curl_exec($ch);
//check for errors in cURL
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
return $output->site_name;//Output /Return : {"site_name":"28e1182c"}
}
function createSubAccount($emailToCreate) {
$first_name = $_REQUEST['first_name'];//First Name
$last_name = $_REQUEST['last_name'];//Last Name
$data = '{"account_name":"'.$emailToCreate.'", "first_name":"'.$first_name.'", "last_name":"'.$last_name.'"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.website.com/api/create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//execute cURL call and get template data
$output = curl_exec($ch);
if(curl_getinfo($ch,CURLINFO_HTTP_CODE) == 204) {
curl_close($ch);
return $emailToCreate;//Expected return HTTP Code: 204 No Content
} else {
curl_close($ch);
$output = 'failed';
return $output;
die('Account creation failed, error: '. $output . '<br/>');
}
}
?>
This is where an Exception comes in handy.
function createSite() {
throw new \Exception('Failed');
}
try {
$createdSite = createSite($_REQUEST['template_id'],$_REQUEST['original_url']);
//echo 'Site Created: ' . $createdSite . '<br/>';
$accountCreated = createSubAccount($_REQUEST['email']);//client email
} catch(\Exception $err) {
echo $err->getMessage();
}
The Exception prevents the rest of the code in the block from executing once thrown.

Creating and inviting a group to an event via graph api

Is there still no way to invite all the group members to an group event? I am able to create the group event via the api, but members are not invited to the new event. If i create the event via the facebook website i have the option to select "invite all group members". I can't seem to find any way to reproduce that functionality via the api.
Since there was no way to do this directly via the graph api i ended up making my own php w/mysql solution. Basically i make a table of all my group's members, then i check if they have been invited to all my group's events. the good thing about this forced method is that if a new members joins after an event is created they will be invited to all future events.
facebook does not tell you how many members you can invite from the api per time period and not get flagged for spam
code to dump group members:
function UpdateFacebookMemberDB($verbatim = FALSE)
{
global $access_token, $groupID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/members?access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
Sql_Connect();
if(!Sql_Query("DELETE FROM `Facebook_User`;"))
{
echo "Could not empty the `Facebook_User` table.<br>\n";
return FALSE;
}
foreach ($decoded as $value)
{
$query="INSERT INTO `Facebook_User` (`Name`, `FID`) VALUES ('".Sql_CleanInput($value['name'])."', '".Sql_CleanInput($value['id'])."');";
if(Sql_Query($query))
{
if($verbatim)
echo $value['name']." was added to the database.<br>";
}
else if($verbatim)
echo $value['name']." was <b>NOT</b> added to the database.<br>";
}
if(!Sql_Query("DELETE FROM `Facebook_Event`;"))
{
echo "Could not empty the `Facebook_Event` table.<br>\n";
return FALSE;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/events?fields=start_time&since=now&access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
foreach ($decoded as $value)
{
$eid = $value['id'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$eid.'/invited?access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decod = json_decode($return, true);
$decod = $decod['data'];
foreach ($decod as $val)
{
$query="INSERT INTO `Facebook_Event` (`EventID`, `UserID`) VALUES ('".$eid."', '".$val['id']."');";
if(Sql_Query($query))
{
if($verbatim)
echo $val['name']." was added to the database.<br>";
}
else if($verbatim)
echo $val['name']." was <b>NOT</b> added to the database.<br>";
}
}
Sql_Disconnect();
}
code to check/invite members:
function InviteClubToEvent)
{
global $access_token, $groupID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$groupID.'/events?fields=start_time&since=now&access_token='.$access_token);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close ($ch);
$decoded = json_decode($return, true);
$decoded = $decoded['data'];
$invite=0;
for($x=count($decoded)-1;$x>=0;$x--)
{
$eventid = $decoded[$x]['id'];
Sql_Connect();
$query="SELECT * from `Facebook_User` ORDER BY `FID` ASC;";
$qresult=Sql_Query($query);
$data = array( 'access_token' => $access_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_USERAGENT , 'facebook-php-3.2');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
for($i=0;$i<Sql_Num_Rows($qresult);$i++)
{
$subquery="SELECT * from Facebook_Event WHERE `UserID` ='".Sql_Result($qresult,$i,"FID")."' AND `EventID` ='".$eventid."'LIMIT 1;";
if(Sql_Num_Rows(Sql_Query($subquery))==0)
{
$invite++;
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$eventid.'/invited/'.Sql_Result($qresult,$i,"FID"));
$return = curl_exec($ch);
if($return == "true")
{
echo "$invite) ".Sql_Result($qresult,$i,"Name") . " has been invited to $eventid!<br>";
Sql_Query("INSERT INTO `sdbmwcca_main`.`Facebook_Event` (`EventID`, `UserID`) VALUES ('".$eventid."', '".Sql_Result($qresult,$i,"FID")."');");
} else
{
echo "<hr><b>$invite) ".Sql_Result($qresult,$i,"Name") . " was not invited because: " . $return . "</b><hr>";
curl_close($ch);
Sql_Disconnect();
die();
}
}
if($invite > ##some spam limit##)
{
$x = 0;
break;
}
}
curl_close ($ch);
Sql_Disconnect();
}
}

how to use curl in php? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
get the value of an url response with curl
I have an php page names stores.php now i want to see the output of this page using curl, what i can do ?
my code is so far for stores.php page
<?php
include_once '../application/Boot.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$body = #file_get_contents('php://input');
$json = json_decode($body, true);
if (isset($json['version'])) {
$client_cache_version = #$json['version'];
$sql = $db->quoteInto("SELECT * FROM stores where version_modified > ". $client_cache_version);
$results = $db->fetchAll($sql);
$version_sql = $db->quoteInto("SELECT max(version_modified) as version FROM stores");
$version_results = $db->fetchAll($version_sql);
$count = array(
'count' => sizeof($results)
);
array_push($results, $version_results['0']);
array_push($results, $count);
//ob_start("ob_gzhandler");
header('HTTP/1.1 200 Stores list');
echo json_encode($results);
exit;
}else {
header('HTTP/1.1 400 Bad Request');
exit;
}
}else{
header('HTTP/1.1 400 Bad Request');
exit;
}
?>
use man curl for how to use curl to display the response of a webpage.
example:
curl "http://www.stackoverflow.com"
function getPage($url, $referer, $agent, $header, $timeout, $proxy="")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($proxy != "")
{
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('cookies.txt'));
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('/cookies.txt'));
$result['EXE'] = curl_exec($ch);
$result['INF'] = curl_getinfo($ch);
$result['ERR'] = curl_error($ch);
curl_close($ch);
return $result;
}
$url = "www.targeturl.com";
$referer = "http;//www.google.com";
$agent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
$header = 1;
$timeout = 15;
$result = getPage($url, $referer, $agent, $header, $timeout);
//$result["ERR"] contain errors if any one
//$result['EXE'] have the html of traget url you supplied in $url variable
//$result['info] have information.
you can use it like this
if(empty($result["ERR"])) // no error
{
echo $result['EXE']; //html of target url
}
else // errors
{
// do something on errors
}
// $proxy is optional
// if you want to open target url through a proxy use it like this
$proxy = "120.232.23.23:8080";
$result = getPage($url, $referer, $agent, $header, $timeout,$proxy);

Categories