Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hello I am having trouble finding a way to detect a string exists on Google's Search.
So if I give the PHP file:
This is plagiarized
It would tell me, to test you can put your query in Google like so: "This is plagiarized", and if it is it would have some results but, if it is original it comes back with no results.
Google has its own api. You can read the documentation here.
An example with the deprecated api:
$search = "some unique contents";
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Referer: http://your_site.com\r\n" // see the terms of service
)
);
$context = stream_context_create($opts);
if (false !== $data = file_get_contents('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="'.urlencode($search).'"', false, $context)) {
$json = json_decode($data, true);
if (!empty($json['responseData']['results'])) {
echo "This is plagiarized!";
}
}
Note that the query its wrappet between ". That's tell google to find the exact phrase.
Better to use Federico's answer, but if it doesn't work for some reason, here is another approach:
$str = "color";
$html = file_get_contents("https://www.google.com.ua/search?q=".urlencode($str));
if (strpos($html,"id=\"resultStats\"></div>") === false) {
echo "yes";
}
else {
echo "no";
}
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In my PHP app I am retrieving data from a REST API (namely PokeAPI) and want to be able to cache the information I retrieve from it in order to cut down on time and API requests. What would be the best way to go about doing this?
Here is the code I am using the API for:
<?php
$base = "http://pokeapi.co/api/v2/pokemon/";
if (isset($_POST["dexno"])) {
$dexarray = $_POST["dexno"];
foreach( $dexarray as $d ) {
$data = #file_get_contents($base.$d);
if ($data != "") {
$pokemon = json_decode($data);
$img = $pokemon->sprites->front_default;
$imageData = base64_encode(file_get_contents($img));
echo '<img src="data:image/png;base64,'.$imageData.'">'.'<br>';
echo $pokemon->name.'<br>';
} }
}
?>
This simply takes numerical data from an array, puts it into a URL and pulls information from the URL generated. Any help would be much appreciated, as I have only just started learning PHP.
Like this:
$json = ""; //your json string.
$fh = fopen("myCacheFile.jsoN" , "w+");
fwrite($fh , $json);
fclose($fh);
If you want short code:
file_put_contents("myCacheFile.json" , $json);
To Retrieve:
$arr = json_decode(file_get_contents("myCacheFile.json") , true);
You can simply store the data in file on disk, and next time before making the call read from disk.
You can store it in database, and read from there.
You can use redis / memcached to store it in memory.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am receiving an external file with strings, I have no control over that file but I receive special characters as this (i think it's unicode?) sequence \u00e1n.
Is there anyway to convert this kind of char sequences to their "readable" counterparts?
Thanks!
EDIT:
I am calling a url that gives me a list with names of persons:
Tom\u00e1nsson\n
Eriksen\n
Gilverto\n
I am reading the names and showing them in my site.
EDIT
Now, I've figured out the correct, working answer based on the article I found. Here is it:
function unicode2utf8($str) {
$a=json_decode(str_replace("\n","<br>",'["'.$str.'"]'));
return str_replace("<br>","\n",$a[0]);
}
PHP:
echo unicode2utf8("Tom\u00e1nsson\n Eriksen\n Gilverto\n");
Output:
Tománsson
Eriksen
Gilverto
ORIGINAL
I've found an article about the same problem here ( http://www.welefen.com/php-unicode-to-utf8.html )
The solution is the following function
function unicode2utf8($str){
if(!$str) return $str;
$decode = json_decode($str);
if($decode) return $decode;
$str = '["' . $str . '"]';
$decode = json_decode($str);
if(count($decode) == 1){
return $decode[0];
}
return $str;
}
I've tried it with
echo unicode2utf8("\u00e1");
Output:
á
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I created a function in PHP first and then following it tried to make the same thing in Python. Following are my codes
Python:
def bfs(my_data):
my_queue = []
my_queue.insert(0, my_data[0]);
my_data[0]['visited'] = '1';
while my_queue:
vertex = my_queue.pop()
print(vertex['letter'])
for n_vertex in vertex['neighbors']:
int_vertex = int(n_vertex)-1
if my_data[int_vertex]['visited'] is '0':
my_data[int_vertex]['visited'] = '1'
test.insert(0, my_data[int_vertex])
my_queue = str(test)
PHP:
function bfs($my_data)
{
$my_queue = array(); //array to store vertices
array_unshift($my_queue, $my_data[0]); // pass the first value to the first index of queue
$my_data[0]['visited'] = true; // value for visited is set to true for the first vertix
//print_r($my_queue);
while(!empty($my_queue))
{
$vertex = array_pop($my_queue); // passing the last value of queue to vertex
echo $vertex['letter'];
$msg = $vertex['letter'];
$output_file = $_POST["output_file_name"];
$output_file_path = "../SPA/" . $output_file;
$outfile = fopen($output_file_path, 'aw'); //writing output to the file
fwrite($outfile, $msg);
fclose($outfile);
// fwrite($outfile, $msg);
foreach($vertex['neighbours'] as $n_vertex)
{
//print_r($n_vertex);
if(!$my_data[$n_vertex-1]['visited'])
{
$my_data[$n_vertex-1]['visited'] = true; // set visited true after visiting each neighbour
array_unshift($my_queue, $my_data[$n_vertex-1]); //pass the neighbours to queue
}
}
}
}
I believe both are same functions but as i am getting different results i am trying to find out the difference. What do you think? Also, if they are different can you tell me how?
This is quite a broad question (what results are you seeing, what do you suspect?) But, for one thing, the for loop in Python is not indented to be within the while, whereas in PHP it is inside the while loop.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
After trying for a long period of time I am a bit stuck... I want to echo a value (url) in a json array with PHP and get it into a variable.
PHP:
$coverphotos = $facebook->api('http://graph.facebook.com/' . $album['id'] . '/picture?type=album&redirect=false&access_token=' . $access_token);
JSON:
{
"data": {
"url": "http://photos-c.ak.fbcdn.net/hphotos-ak-ash4/480080_567808343263483_176928672_a.jpg"
}
}
I want to get the actual url into a php variable... how would I go about doing this?
HOLY THAT TOOK LONG ENOUGH BUT I FIGURED IT OUT:
$coverphotos2 = $coverphotos['id'] . '?type=album&redirect=false&access_token=' . $access_token;
$content = file_get_contents($coverphotos2);
$photoarray = json_decode($content, TRUE);
$coverurl = $photoarray['data']['url'];
From the $coverphotos variable, you can store the URL of the picture with:
$picture = $coverphoto['id'];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
must create a rule that captures the first preg_replace bar url:
http://localhost/item/other
must take the rule just 'item' regardless of what comes after
You should use the php parse_url() function
An example would be:
$parts = parse_url("http://localhost/item/other");
$path_parts= explode('/', $parts[path]);
$item = $path_parts[1];
echo $item;
It does not look like you have a specific question. I am assuming you are about write some routes script.
$request_uri = explode('/', $_SERVER['REQUEST_URI']);
$delimiter = array_shift($request_uri);
$controller = array_shift($request_uri);
$action = array_shift($request_uri);
if(preg_match('/item/i', $controller))
{
// do something right
}
else
{
// do something wrong
}