Faceting with solr php client - php

I am trying to get the faceting results and I don't know how. there are no documentation for solr-php-client. In this example, print_r( $response->getRawResponse() ) does not include faceting. I am not sure weather faceting is not successfully requested or it is a matter of accessing the faceting data in the responce. Please also help me if there is any documentation available online for solr-php-client.
An other question is how to declare "user/password" if solr is protected when initiating the instance of solr-php-client
require_once( 'solr-php-client/Apache/Solr/Service.php' );
$solr = new Apache_Solr_Service( 'localhost', '8983', '/solr/products' );
if ( ! $solr->ping() ) {
echo 'Solr service not responding.';
exit;
}
$offset = 0;
$limit = 10;
$queries = array(
'name:iphone'
);
$additionalParameters = array(
'facet' => 'true',
'facet.field' => array(
'category'
)
);
foreach ( $queries as $query ) {
$response = $solr->search( $query, $offset, $limit );
if ( $response->getHttpStatus() == 200 ) {
print_r( $response->getRawResponse() );
if ( $response->response->numFound > 0 ) {
echo "$query <br />";
foreach ( $response->response->docs as $doc ) {
echo "$doc->id $doc->name <br />";
}
echo '<br />';
}else{
echo "zero results";
}
}
else {
echo $response->getHttpStatusMessage();
}
}

I missed to add $additionalParameters to the search arguements;
$response = $solr->search( $query, $offset, $limit,$additionalParameters );

Related

Get post count from instagram api 2021

I was using this code until yesterday and it ran without any problems
But it does not work any more
Get post count from instagram api
<?php
$username = 'instagram';
$response = #file_get_contents( "https://www.instagram.com/$username/?__a=1" );
if ( $response !== false ) {
$data = json_decode( $response, true );
if ( $data !== null ) {
$full_name = $data['graphql']['user']['full_name'];
$follower = $data['graphql']['user']['edge_followed_by']['count'];
$follows = $data['graphql']['user']['edge_follow']['count'];
$posts = $data['graphql']['user']['edge_owner_to_timeline_media']['count'];
echo "<h2><a href='https://www.instagram.com/{$username}'>{$full_name}</a></h2>
<p><span>{$posts} posts</span> <span>{$follower} followers</span> <span>{$follows} following</span></p>";
}
} else {
echo 'Username not found.';
}
?>

unable to fetch Instgram followers count

I want to fetch the count of Instagram followers but unable to fetch, i am implementing below code to fetch the number.
<?php
$username = 'shubhamtester14';
$response = #file_get_contents( "https://www.instagram.com/$username/?__a=1" );
if ( $response !== false ) {
$data = json_decode( $response, true );
if ( $data !== null ) {
$full_name = $data['graphql']['user']['full_name'];
$follower = $data['graphql']['user']['edge_followed_by']['count'];
echo "{$full_name} have {$follower} followers.";
}
} else {
echo 'Username not found.';
}
?>

the difference between Solr Client and Apache_Solr_Service?

Here is my code which works well:
<?php
if(isset($_POST['btn'])){
$search=$_POST['search'];
require_once('Apache/Solr/Service.php');
$solr=new Apache_Solr_Service( 'localhost', '8983', '/solr' );
$offset = 0;
$limit = 10;
$queries = array(
'title:*'.$search.'*'
);
foreach ($queries as $query) {
$response = $solr->search($query, $offset, $limit);
if ($response->getHttpStatus() == 200) {
// print_r( $response->getRawResponse() );
if ($response->response->numFound > 0) {
foreach ($response->response->docs as $doc) {
echo '<pre>';
echo 'title ='.$doc->title;
echo '<br>';
echo 'image='.$doc->image;
echo '</pre>';
}
echo '<br />';
}
} else {
echo $response->getHttpStatusMessage();
}
}
}
?>
It searches for what $_POST['search'] is. But I want to use something named Solr Client. Because it has a method named $solrQuery->set('spellcheck','true'); that gives you the "did you mean x ?" feature.
I've followed this from the doc with no success. It throws SolrClient class not found.
Any clue how can I use SolrClient and what's its different with Apache_Solr_Service ?

Trying to get property of non-object error in PHP

I'm trying to save my data using php. This is how I tried:
<?php
session_start();
if(!isset($_SESSION["MemberID"])) header("Location:logout.php");
include '../config.php';
include '../momdb.php';
$page_title = "Customers";
$mode = "Save";
$db = 'db';
$showmessage = false;
$message = "";
if (isset ( $_POST ["btnSave"] )) {
$data = new table ();
$data->MomID = $_POST ["CustomerID"];
$data->CustomerName = $_POST ["CustomerName"];
$data->FromDate = string_to_date ( $_POST ["FromDate"] );
$data->ToDate = string_to_date ( $_POST ["ToDate"] );
$data->CreatedBy = $_SESSION["MemberID"];
$data->CreatedDatetime = date ( "Y-m-d H:i:s" );
$data->LastModifiedBy = $_SESSION["MemberID"];;
$data->LastModifiedDatetime = date ( "Y-m-d H:i:s" );
$rows = array ();
if (intval ( $data->CustomerID) > 0) {
$rows = $db::Update ( $data );
} else {
$rows = $db::Save ( $data );
}
if (intval ( $rows ["WebID"] ) > 0) {
$date = new DateTime ();
header ( "Location: customerlist.php?msg=yes&msgt=s&t=" . $date->getTimestamp () . "&mtext=Record saved successfully" );
} else {
$showmessage = true;
$message = $rows ["Message"];
}
} elseif (isset ( $_GET ["CustomerID"] )) {
$data = $db::GetByID ( $_GET ["CustomerID"] );
if ($data == false) {
$data = new table ();
$data->CustomerID = 0;
}
}
if ($data->CustomerID == "0")
$mode = "Save";
else
$mode = "Update";
?>
This code is for sending my values to the db page for saving them in the DB. I've used the PDO method. This is the first time I'm saving the record this way. That's why there is lots of confusion. The following code shows the Save() method, which I used to save the values to the DB.
public function Save(table $customer) {
$data = new DB ();
$stmt = $data->connection->prepare ( "CALL proc_CreateCustomer (:CustomerName, :FromDate,:ToDate,:CreatedBy, :CreatedDatetime, :LastModifiedBy, :LastModifiedDatetime)" );
$stmt->bindParam ( ':CustomerName', trim ($customer->CustomerName ));
$stmt->bindParam ( ':FromDate', $customer->FromDate );
$stmt->bindParam ( ':ToDate', $customer->ToDate);
$stmt->bindParam ( ':CreatedBy',$customer->CreatedBy );
$stmt->bindParam ( ':CreatedDatetime', $customer->CreatedDatetime );
$stmt->bindParam ( ':LastModifiedBy',$customer->LastModifiedBy );
$stmt->bindParam ( ':LastModifiedDatetime', $customer->LastModifiedDatetime );
$stmt->execute ();
$record = $stmt->fetch ( PDO::FETCH_OBJ );
if ($record == false) {
return array (
"WebID" => 0,
"Message" => $record->Message,
"AppID" => $customer->AndroidID
);
} else {
return array (
"WebID" => $record->CustomerID,
"Message" => $record->Message,
"AppID" => $customer->AndroidID
);
}
}
This is how I've tried. But, whenever I try to save the details, it shows: " Trying to get property of non-object in D:\Workspace\Application\momdb.php on line 39", ie it shows error in the line "Message" => $record->Message,"AppID" => $customer->AndroidID. I don't what's wrong with this. Can someone tell me what should I change so that it work fine?
if ($record == false) {
return array (
"WebID" => 0,
"Message" => $record->Message,
"AppID" => $customer->AndroidID
);
}
In this piece of code $record is false(y), so $record->Message doesn't exist.

File Writing using PHP

I am working on a project in my college which is about developing an online autograder website like that of hackerrank for my college. Now, for compiling codes, I am using the API provided by sphere engines. The API is returning the output as a string which then I store it into a file. Then, I use a previously defined output file to compare this file and check accordingly whether the code is right or wrong. Now the problem is for eg. if output is like below:
Hello World
Hello World
Hello World
The string is written in the file as Hello WorldHello WorldHwllo World. I am using PHP to write text to file.
Is there any way to write the string as it is to a file?
Any other alternative method will also be appreciated.
Thank You.
EDIT
for eg if the code:
#include<iotsream>
using namespace std;
int main()
{
int i;
for(i=0;i<3;i++)
{
cout<<"Hello World\n";
}
return 0;
}
The output of this code should be
Hello World
Hello World
Hello World
And when i am displaying the string which is stored in a PHP array $data['output'], it is getting displayed normally but when i am writing this string to a file, It is getting stored in a single line. I want to store the string as it is i.e. in different lines.
Due to this, this file if not equal to the template output file which contains the string Hello World in different lines.
EDIT
This is the PHP code for the file which will take the source code and input and send it to the compiler and receive it's output accordingly.
<?php
error_reporting(0);
include_once '../connection-script.php';
session_start();
$user = 'xxxxx';
$pass = 'xxxxx';
$code = '';
$input = '';
$run = true;
$private = false;
$subStatus = array(
0 => 'Success',
1 => 'Compiled',
3 => 'Running',
11 => 'Compilation Error',
12 => 'Runtime Error',
13 => 'Timelimit exceeded',
15 => 'Success',
17 => 'memory limit exceeded',
19 => 'illegal system call',
20 => 'internal error'
);
$error = array(
'status' => 'error',
'output' => 'Something went wrong :('
);
//echo json_encode( array( 'hi', 1 ) ); exit;
//print_r( $_POST ); exit;
if ( isset( $_POST['process'] ) && $_POST['process'] == 1 ) {
$lang = isset( $_POST['lang'] ) ? intval( $_POST['lang'] ) : 1;
$input = trim( $_POST['input'] );
$code = trim( $_POST['source'] );
$answerfile=$_POST['answerfile'];
$outputfile=$_POST['outputfile'];
$client = new SoapClient( "http://ideone.com/api/1/service.wsdl" );
//create new submission
$result = $client->createSubmission( $user, $pass, $code, $lang, $input, $run, $private );
//if submission is OK, get the status
if ( $result['error'] == 'OK' ) {
$status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
if ( $status['error'] == 'OK' ) {
//check if the status is 0, otherwise getSubmissionStatus again
while ( $status['status'] != 0 ) {
sleep( 3 ); //sleep 3 seconds
$status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
}
//finally get the submission results
$details = $client->getSubmissionDetails( $user, $pass, $result['link'], true, true, true, true, true );
if ( $details['error'] == 'OK' ) {
//print_r( $details );
if ( $details['status'] < 0 ) {
$status = 'waiting for compilation';
} else {
$status = $subStatus[$details['status']];
}
$data = array(
'status' => 'success',
'meta' => "Status: $status | Memory: {$details['memory']} | Returned value: {$details['status']} | Time: {$details['time']}s",
'output' => htmlspecialchars( $details['output'] ),
'raw' => $details
);
if( $details['cmpinfo'] ) {
$data['cmpinfo'] = $details['cmpinfo'];
}
$myfile=fopen("output.txt","w");
fwrite($myfile, $data['output']);
fclose($myfile);
$qid=$_POST['questionid'];
$did=$_POST['domainid'];
if(sha1_file("output.txt") == sha1_file($outputfile))
{
$file=fopen($answerfile,"w");
fwrite($file,$code);
fclose($file);
$mail=$_SESSION['email'];
$query="SELECT * from student where semail='$mail'";
$result1=mysql_query($query);
$row1=mysql_fetch_array($result1);
$sid=$row1['studentid'];
$sql="SELECT * from practiceques where did='$did' and quesid='$qid'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$marks=$row['marks'];
$curdate=date("Y-m-d H:i:s");
$answer=mysql_query("INSERT INTO points VALUES ('$sid','$qid','$curdate',1,'$marks','$did')");
echo json_encode( $data );
}
else
{
$mail=$_SESSION['email'];
$query="SELECT * from student where semail='$mail'";
$result1=mysql_query($query);
$row1=mysql_fetch_array($result1);
$sid=$row1['studentid'];
$sql="SELECT * from practiceques where did='$did' and quesid='$qid'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$marks=$row['marks'];
$curdate=date("Y-m-d H:i:s");
$answer=mysql_query("INSERT INTO points VALUES ('$sid','$qid','$curdate',0,0,'$did')");
echo json_encode($data);
}
}
else {
//we got some error :(
//print_r( $details );
echo json_encode( $error );
}
}
else {
//we got some error :(
//print_r( $status );
echo json_encode( $error );
}
} else {
//we got some error :(
//print_r( $result );
echo json_encode( $error );
}
}
?>
You can't write with C in php document.
Anyway, This is the php code for write in file.
<?php
$fp = fopen('file.name', 'a+');
fwrite($fp, 'Hello World'.PHP_EOL);
fclose($fp);
?>

Categories