Yahoo Search API Problem - php

I am having problem with the yahoo search API, sometimes it works and sometimes don't why I am getting problem with that
I am using this URL
http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start
The code is given below:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<? $search = $_GET["search"];
$replace = " "; $with = "+";
$search = str_replace($replace, $with, $search);
if ($rs =
$rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start")
)
{ }
// Go through the list powered by the search engine listed and get
// the data from each <item>
$colorCount="0";
foreach($rs['items'] as $item) { // Get the title of result
$title = $item['title']; // Get the description of the result
$description = $item['description']; // Get the link eg amazon.com
$urllink = $item['guid'];
if($colorCount%2==0) {
$color = ROW1_COLOR;
} else {
$color = ROW2_COLOR;
}
include "resulttemplate.php"; $colorCount++;
echo "\n";
}
?>
Sometimes it gives results and sometimes don't. I get this error usually
Warning: Invalid argument supplied for foreach() in /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php on line 14
Can anyone help..

The error Warning: Invalid argument supplied for foreach() in /home4/thesisth/public_html/pdfsearchmachine/classes/rss.php on line 14 means the foreach construct did not receive an iterable (usually an array). Which in your case would mean the $rs['items'] is empty... maybe the search returned no results?
I would recommended adding some checks to the results of $rss->get("...") first, and also having an action for when the request fails or returns no results:
<?php
$search = isset($_GET["search"]) ? $_GET["search"] : "default search term";
$start = "something here"; // This was left out of your original code
$colorCount = "0";
$replace = " ";
$with = "+";
$search = str_replace($replace, $with, $search);
$rs = $rss->get("http://api.search.yahoo.com/WebSearchService/rss/webSearch.xml?appid=yahoosearchwebrss&query=originurlextension%3Apdf+$search&adult_ok=1&start=$start");
if (isset($rs) && isset($rs['items'])) {
foreach ($rs['items'] as $item) {
$title = $item['title']; // Get the title of the result
$description = $item['description']; // Get the description of the result
$urllink = $item['guid']; // Get the link eg amazon.com
$color = ($colorCount % 2) ? ROW2_COLOR : ROW1_COLOR;
include "resulttemplate.php";
echo "\n";
$colorCount++;
}
}
else {
echo "Could not find any results for your search '$search'";
}
Other changes:
$start was not declared before your $rss->get("...") call
compounded the $color if/else clause into a ternary operation with fewer comparisons
I wasn't sure what the purpose of the if ($rs = $rss->get("...")) { } was, so I removed it.
I would also recommend using require instead of include as it will cause a fatal error if resulttemplate.php doesn't exist, which in my opinion is a better way to detect bugs than PHP Warnings which will continue execution. However I don't know you whole situation so it might not be of great use.
Hope that helps!
Cheers

Related

PHP foreach code performance speed

I am trying to optimize increase the speed of this loop. I would like it if anyone has any ideas to boost the speed as there is thousands of members and it does take time time to complete currently
SQL Stored PROCEDURE MembersMobileByVenue returns this->
Example
FirstName Phone Venue
Aaron 04******* 7272CD46D51F
Brad 04******* CF105BB0
Adam 04******* 7272CD46D51F
Craig 04******* CF105BB0
PHP
$venueIDS = isset($_POST['location']) ? $_POST['location'] : array();
$msg = $_POST['message'];
$response = array();
if(!empty($venueIDS)){
$Members = GoldCardMembers::MembersMobileByVenue();
foreach($Members as $Member){
if(in_array($Member->Venue, $venueIDS)){
$destination = $Member->Phone;
$text = 'Hi ' . $Member->FirstName . ' ' .$msg. '. Reply STOP to opt out';
$ref = 'Members';
$content = '&to='.rawurlencode($destination).
'&message='.rawurlencode($text).
'&ref='.rawurlencode($ref);
$smsbroadcast_response = sendSMS($content);
$response_lines = explode("\n", $smsbroadcast_response);
foreach( $response_lines as $data_line){
$message_data = "";
$message_data = explode(':',$data_line);
if($message_data[0] == "OK"){
array_push($response, "The message to ".$message_data[1]." was successful, with reference ".$message_data[2]."\n");
}elseif( $message_data[0] == "BAD" ){
array_push($response, "The message to ".$message_data[1]." was NOT successful. Reason: ".$message_data[2]."\n");
}elseif( $message_data[0] == "ERROR" ){
array_push($response, "There was an error with this request. Reason: ".$message_data[1]."\n");
}
}
}
}
}
foreach($response as $message){
echo $message;
}
$venueIDS = isset($_POST['location']) ? $_POST['location'] : array();
$msg = $_POST['message'];
$text = "";
$destination = "";
$content = "";
$response_lines = array();
$smsbroadcast_response = "";
$message_data = array();
if(!empty($venueIDS)){
$Members = GoldCardMembers::MembersMobileByVenue();
foreach($Members as $Member){ // O(n) size of $Members
if(in_array($Member->Venue, $venueIDS)){ // O(v) size of $venueIDs
$destination = rawurlencode($Member->Phone);
$text = rawurlencode($Member->FirstName . ' ' .$msg);
$content = '&to='. $destination .
'&message=Hi ' . $text . '. Reply STOP to opt out'.
'&ref=Members';
$smsbroadcast_response = sendSMS($content);
$response_lines = explode("\n", $smsbroadcast_response); // O(r) number of "\n" occurances
foreach( $response_lines as $data_line){ // O(r)
$message_data = explode(':',$data_line); // O(d) number of ":" occurances
if($message_data[0] == "OK"){
echo "The message to ".$message_data[1]." was successful, with reference ".$message_data[2]."\n";
}elseif( $message_data[0] == "BAD" ){
echo "The message to ".$message_data[1]." was NOT successful. Reason: ".$message_data[2]."\n";
}elseif( $message_data[0] == "ERROR" ){
echo "There was an error with this request. Reason: ".$message_data[1]."\n";
}
}
}
}
}
Here's a list of what I did:
removed the for loop from bottom and echo the $message directly from the main loop instead
removed $ref var
initialized $destination, $text, $content, $response_lines, $smsbroadcast_response and $message_data vars outside the for loop since declaring them inside the loop with each iteration is sub-optimal
That's all I can think about for now.
Notes for further improvements:
I'd investigate on the performance of sendSMS that you're using. It could be one of the components that are slowing things down.
In order to dig deeper, a helpful tool for analyzing performance is big Oh notation. So I've added comments to label loop components.
Because we're looking at multiple nested loops we generally have something like O(N^3) here.
However, we could write that equation to be more detailed such as this:
O( n * (v + 2r) * d)
(please check code comments to see what each variable represents)
Since you're asking for a high "n" (number of members) I'd investigate ways to make the other variables like "v", "r" or "d" smaller.
Eliminating any of these variables altogether like removing
$message_data = explode(':',$data_line);
have the highest positive impact on performance (given that is still achieves the same functionality).

PHP:Trying to get property of non-object

There are many posts on this topic but I did not get the required answer. Hence, I am here.
I have been getting Notice: Trying to get property of non-object in /opt/lampp/htdocs/amit/crawlnepalstock.php on line 49 error in my php page.
Here is my code
<?php
include_once('simple_html_dom.php');
error_reporting(E_ALL);
$html = file_get_html('http://nepalstock.com/datanepse/index.php');
$indexarray = array('s_no','stocksymbol', 'LTP', 'LTV', 'point_change', 'per_change', 'open','high', 'low', 'volume','prev_close');
$stocks = array();
$maincount = 0;
$tables = $html->find('table[class=dataTable]');
$str = $html->plaintext;
$matches = array();
foreach ($tables[0]->find('tr') as $elementtr) {
$count = 0;
$temp = array();
$anchor = $elementtr->children(1)->find('a',0);
$splits = preg_split('/=/', $anchor->href); **//line 49**
$temp['stocksymbol'] = isset($splits[1]) ? $splits[1] : null;
$temp['fullname'] = $elementtr->children(1)->plaintext;
$temp['no_of_trans'] = $elementtr->children(2)->plaintext;
$temp['max_price'] = $elementtr->children(3)->plaintext;
$temp['min_price'] = $elementtr->children(4)->plaintext;
$temp['closing_price'] = $elementtr->children(5)->plaintext;
$temp['total_share'] = $elementtr->children(6)->plaintext;
$temp['amount'] = $elementtr->children(7)->plaintext;
$temp['previous_close'] = $elementtr->children(8)->plaintext;
$temp['difference'] = $elementtr->children(9)->plaintext;
$stocks[] = $temp;
}
$html->clear();
unset($html);
echo '<pre>';
print_r($stocks);
echo '</pre>';
?>
I have not included simple_html_dom.php class as it is quite long. Your opinions are very much appreciated. You can find simple_html_dom.php file online in case http://sourceforge.net/projects/simplehtmldom/files/
You are trying to access property of non-object or from null object. e.g.
$obj = null;
echo $boject->first_name // this will produce same error as you are getting.
// another example may be
$obj = array();
echo $obj->first_name; // this will also produce same error.
In code sample Line 49 is not clear so you should check yourself this type of error on line 49.
This is happening because there is no longer a td[align="center"] tag found in the google.com document. Perhaps it was there when the code was first written.
So, what the others are saying about a non-object is true, but because the HTML was not found, there is not an object to use the ->plaintext method on.
As of 12/11/2020, if you change the URL found in line 6 of example_basic_selector.php to this:
$html = file_get_html('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_td_align_css');
And change this line
echo $html->find('td[align="center"]', 1)->plaintext . '';
to:
echo $html->find('td style="text-align:right"', 1)->plaintext. '';
the error will go away because the text it searches for is found, and thus the method works as intended.

Fastest way to retrieve part from JSON in my case

Now, I use strstr to get data from external JSON file. I'm not sure if it's the fastest way to do that what I want and I can't test because json_decode don't work in my code.
$before = '"THIS":"';
$after = '","date"';
$data = strstr(substr($url, strpos($url, $before) + strlen($before)), $after, true)
and with json_decode:
$address = file_get_contents('http://json.link/?something=Hello');
$data = json_decode($address);
echo $data->data->THIS;
Now, when I replace my first code with second I get:
Notice: Trying to get property of non-object
All my code:
$text = "a lot of text";
$text_split = array(0 => '');
$number = 0;
$words = explode(' ', $text);
foreach($words as $word)
{
if(strlen($text_split[$number]) + strlen($word) + 1 > 500)
{
++$number;
$text_split[$number] = '';
}
$text_split[$number] .= $word.' ';
}
foreach($text_split as $texts)
{
$text_encode = rawurlencode($texts);
$address = file_get_contents('http://json.link/?something='.$text_encode);
$data = json_decode($address);
echo $data->data->THIS;
}
What should in do in that case? Keep using strstr or replace all code to work with json_decode (maybe because execution time is faster?)? If the second option, how I can make json_decode work here? Thanks!
... and sorry for bad english.
LE:
If I replace $address = file_get_contents('http://json.link/?something='.$text_encode); with $address = file_get_contents('http://json.link/?something=Hello'); I get VALID result for "Hello" text but 10 times. I guess because it's in a foreach.
json_decode is the suggested method to work with JSON data. Here I think you are trying to access an invalid property in JSON object.
$data = json_decode($address);
echo $data->data->THIS;
I guess you need $data->date instead of $data-data?
you have to access the specific key value like this
$json = '{"success":true,"msg":"success","data":{"THIS":"thing I need","date":"24.03.2014","https":false}}';
$d=json_decode($json,true);
echo $d['data']['THIS'];

PHP find keywords in XML feed - preg_match_all

I'm loading in a rss feed in XML. I am then attempting to search the RSS feed for keywords. I then need to it email myself with a notification that it found these key words and their contents.
Example i have:
$keywords = "iphone|ipad|ipod";
$rss = simplexml_load_file("http://myfeed.com/news?format=xml");
foreach ($rss->entry as $i)
{
//need to check the title tag for the keywords
//email myself a notification with the keywords found
}
UPDATE
Ok i have the following which is part working:
function getPartsWithRegex($text, $regex){
preg_match_all($regex, $text, $array);
return $array[0];
}
$keywords = "LLC|socks";
$rss = simplexml_load_file("http://feeds.myfeed.com/iphoneapps-news?format=xml");
$text = "Downcast - Jamawkinaw Enterprises LLC";
$regex = "/$keywords/i";
foreach ($rss->entry as $i)
{
$title = (string)$i->title;
$productNumbers = getPartsWithRegex($text, $regex);
}
if(count($productNumbers) > 0){
foreach($productNumbers as $productNumber){
echo $productNumber . '<br />';
}
}
else {
echo "Nothing found.";
}
This works when i pass $productNumbers = getPartsWithRegex($text, $regex); However, when i pass $title which has the value of $i->title it doesnt work. Even though when i echo out the value of $title to the browser it renders as: Downcast - Jamawkinaw Enterprises LLC
So to clarify when i manually enter and pass a string it works correct. However, when i pass the value from the XML which is read it doesnt. I've tried converting to string but that doesnt work.
Any ideas why?

Checking if an index in a multidimensional array is an array

The code:
$row['text'] = 'http://t.co/iBSiZZD4 and http://t.co/1rG3oNmc and http://t.co/HGFjwqHI and http://t.co/8UldEAVt';
if(preg_match_all('|http:\/\/t.co\/.{1,8}|i',$row['text'],$matches)){
foreach($matches[0] as $value){
$headers = get_headers($value,1);
if(is_array($headers['Location'])){
$headers['Location'] = $headers['Location'][0];
}
$row['text'] = preg_replace('|http:\/\/t.co\/.{1,8}|i', '' . $headers['Location'] . '',$row['text']);
}
}
This is related to get_headers(). Sometimes get_headers($url,1) returns an array with a location index key like so: [Location]=>Array([0]=>url1 [1]=>url2). I basically want to make [Location] equal to [Location][0] if [Location][0] exists. However, the above code doesn't seem to accomplish that task. I've also tried array_key_exists() and isset() but neither solved the problem. Thoughts?
Don't try to replace on the fly. First get all the values, and then do the replace in one batch (using two arrays as the $search and $replace parameters).
<?php
$replace = array();
$row = array('text' => 'http://t.co/iBSiZZD4 and http://t.co/1rG3oNmc and http://t.co/HGFjwqHI and http://t.co/8UldEAVt');
if (preg_match_all('|http:\/\/t.co\/.{1,8}|i', $row['text'], $search)) {
foreach ($search[0] as $value) {
$headers = get_headers($value, 1);
if (is_array($headers['Location'])) {
$headers['Location'] = $headers['Location'][0];
}
$replace[] = "<a href='{$headers["Location"]}'>{$headers["Location"]}</a>";
}
$row['text'] = str_replace($search[0], $replace, $row['text']);
echo $row["text"];
}
P.S. - Next time, please tell us the context of your problem, tell us you "are making a service that resolves shortened URLs", don't let me figure that out from your code alone.

Categories