I have the following PHP Code:
<?php
$file = "Links.txt";
$parts = new SplFileObject($file); // this is your array of words
foreach($parts as $word) {
$content = file_get_contents($word);
parse_str($content, $ytarr);
echo $ytarr['title'];
unset($content);
}
?>
Please note:
The Links.txt file includes multiple external URL's, on each line is only one URL. Example:
www.External-URL-number-ONE.com
www.External-URL-number-TWO.com
www.External-URL-number-THREE.com
Each of this URL have the 'title' item in the variable $content (after filling it by "file_get_contents($word);".
For troubleshooting purpose, I tested each URL by adding it in the "links.txt" single. The result was for each URL successful. The issue occours, if I add multiple URL's. In that case, the behavior is:
Error message and result:
Notice: Undefined index: title in C:\xampp\htdocs\PHPexample\index.php on line 13
Display the Title of "www.External-URL-number-THREE.com"
How can I fix this problem? It should work also with multiple lines.
Thanks in advance.
EDIT:
The content of the variable $content is:
Array (
[reason] => Invalid parameters.
[status] => fail
[errorcode] => 2
)
Array (
[ISD] => 928398
[enable] => 1
[list] => 39/9339/30
[AMP] =>
[host] =>
[title] => This_Is_the_Title_Three
[token] => 1
)
UPDATE
I have used the isset() for checking the array before access it. And only the last for each loop have an index.
Read the file and read line by line
<?PHP
$file = "Links.txt";
$handle = #fopen($file, "r");
if ($handle) {
// Read line by line
while (($word = fgets($handle)) !== false) {
$content = file_get_contents($word);
// parse_str($content, $ytarr); // parse_str don't work in this case
//echo #$ytarr['title'];
//unset($content);
echo getDataTag($content, 'title');
}
fclose($handle);
}
//This is a dirty solution
function getDataTag($str, $tag){
$str = str_replace('+',' ',$str);
$data = explode(($tag.'='),$str);
$data = explode('&',$data[1]);
return $data[0];
}
?>
Related
I'm trying to write a json file with data gathered via get requests. The json file is a 2D array of strings, but when the data is written to the file, the nested array is written twice.
<?php
//used for parsing html
include("simple_html_dom.php");
//read the file
$fp = fopen("j.json", "r");
$t = fread($fp, filesize("j.json"));
fclose($fp);
$loaded = json_decode($t);
//print the loaded array
print_r($loaded);
//gathering the data
$url = "https://www.soldionline.it/quotazioni/dettaglio/IT0003934657.html";
$prezzo1 = file_get_html($url)->find("span[class=val] b", 0)->plaintext;
$data = file_get_html($url)->find("span[class=ora] b", 0)->plaintext;
$url = "https://www.soldionline.it/quotazioni/dettaglio/IT0003934657.html";
$prezzo2 = file_get_html($url)->find("span[class=val] b", 0)->plaintext;
$url = "https://www.soldionline.it/quotazioni/dettaglio/IT0003934657.html";
$prezzo3 = file_get_html($url)->find("span[class=val] b", 0)->plaintext;
//adding the new data to the array
array_push($loaded, array($prezzo1, $prezzo2, $prezzo3, $data));
//the new json string is parsed and ready to be written
$s = json_encode($loaded);
//printing stuff to ensure the data is correct
echo "<br>".$s.", type=".gettype($s)."<br>";
print_r($loaded);
//write the new json string to the same file
$fp = fopen("j.json", "w");
fwrite($fp, $s);
fclose($fp);
?>
j.json before the script runs:
[]
What the script prints:
Array ( )
[["128,54","128,54","128,54","30\/12"]], type=string
Array ( [0] => Array ( [0] => 128,54 [1] => 128,54 [2] => 128,54 [3] => 30/12 ) )
j.json after the script:
[["128,54","128,54","128,54","30\/12"],["128,54","128,54","128,54","30\/12"]]
I tried opening the file like this: $fp = fopen("j.json", "r+"); and at the and i changed the script:
$s = "\"".json_encode($loaded)."\"";
echo "<br>".$s.", type=".gettype($s)."<br>";
print_r($loaded);
fwrite($fp, $s);
fclose($fp);
And I found out that a null is being written too:
[]"[["128,54","128,54","128,54","30\/12"]]""null"
The browser sends two requests when visiting a url, a request to the php file and another request to /favicon.ico. The second request is send to check if the site has a favicon. This second requests causes the script to execute twice.
The request for the favicon can be prevented by following the steps described here: https://stackoverflow.com/a/38917888/6310593
I am having trouble when trying to use my Livezilla chatbot script to talk to my REST API, both of which I have stored on my local PC. I am using a code snippet very similar from the Livezilla chatbot API page and have modified it, but I am getting some errors.
27.02.19 11:34:11 ::1 ERR# 129 Error connecting USER API, invalid response:
http://localhost/livezilla/programytalk.php (
Notice: Trying to get property 'answer' of non-object in
C:\xampp\htdocs\livezilla\programytalk.php on line 11
) IN LINE 0
the code for programytalk.php is as follows:
<?php
$requestobj = json_decode($_POST["livezilla_user_api_request"]);
$responseNode = array();
$responseNode["ResponseTo"] = "";
$responseNode["Id"] = rand(1111111,9999999);
$responseNode["SearchKB"] = false;
$url = "http://localhost:8989/api/rest/v1.0/ask? question=".rawurlencode($requestobj->Value)."&userid=".$requestobj-
>VisitorId;
$sdata = json_decode(file_get_contents($url));
$responseNode["Value"] = $sdata->answer;
if(!empty($responseNode["Value"]))
echo json_encode($responseNode);
?>
This is the JSON format the API responds in:
[{"response":{"answer":"Good morning.","question":"hello world","userid":"1234567890"}},200]
By that API response you don't need to read
$responseNode["Value"] = $sdata->answer;
instead of that you need to read
$responseNode["Value"] = $sdata[0]->response->answer;
because answer is nested under response...
Hint: just do this
$data = json_decode('[{"response":{"answer":"Good morning.","question":"hello world","userid":"1234567890"}},200]');
print_r($data);
and output will be:
Array
(
[0] => stdClass Object
(
[response] => stdClass Object
(
[answer] => Good morning.
[question] => hello world
[userid] => 1234567890
)
)
[1] => 200
)
Please do this;
<?php
$requestobj = json_decode($_POST["livezilla_user_api_request"]);
$responseNode = array();
$responseNode["ResponseTo"] = "";
$responseNode["Id"] = rand(1111111,9999999);
$responseNode["SearchKB"] = false;
$url = "http://localhost:8989/api/rest/v1.0/ask? question=".rawurlencode($requestobj->Value)."&userid=".$requestobj-
>VisitorId;
$sdata = json_decode(file_get_contents($url), true);
$responseNode["Value"] = $sdata->answer;
if(!empty($responseNode["Value"]))
echo json_encode($responseNode, true);
?>
I have probably spent all day trying to figure this out. I have read multiple questions here on stack and also have been reading articles and checking on documentation, but I can't seem to figure out why this batch of code just produces a null output. Am I missing brackets, calling something wrong, ect?
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str);
$temp = $json['main']['temp_min'];
$content = $temp;
$array = array(
"content" => $content,
refresh_requency => 30
);
echo json_encode($array);
?>
Again what I'm asking is can someone point out to me or tell me what I'm doing wrong. Is it my server that's just not handling the data correctly? That could be a possibility.
One other thing I've tried is to just print out $temp and/or the other variable like $str. When I do that though they don't even show up so that's what I think my problem is just not sure how to fix it.
Update
I've come to the conclusion that it's my web hosting service. As if I add var_dump($json) I get a null output null output.
Also to confirm that its my webhost if I run error_reporting(E_ALL); ini_set('display_errors', 1); it points to the file php.ini not allowing outgoing connections. I edited that same file on my local home server(raspberry pi) ran the same file and it works fine.
Below is the working solution for your above code:
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);
$temp = $json['main']['temp_min'];
$content = $temp;
$array = array(
"content" => $content,
"refresh_requency" => 30
);
echo json_encode($array);
?>
When I executed your code, I found 2 problem into your code snippet:
1) You were trying to use object of type stdClass as array.
Solution:
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str);
$temp = $json->main->temp_min;
$content = $temp;
$array = array(
"content" => $content,
"refresh_requency" => 30
);
echo json_encode($array);
?>
2) You did not put array key into quotes:
$array = array(
"content" => $content,
refresh_requency => 30
);
It should be :
$array = array(
"content" => $content,
"refresh_requency" => 30
);
Access $temp like this
$temp = $json->main->temp_min;
you will get the desired output.
Also, you need to allow allow_url_fopen in your php.ini config file. Some hosts disallow it for security reasons
$json = json_decode($str, true);
You need second argument to convert json string into associative array instead of the object. And you are trying to use array ($json['main']['temp_min']), not object. Also
$array = array(
"content" => $content,
"refresh_requency" => 30
);
The code looks like
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);
$content = $json['main']['temp_min'];
$array = array(
"content" => $content,
"refresh_requency" => 30
);
echo json_encode($array);
And result is http://codepad.viper-7.com/euBNAk :
{"content":44.6,"refresh_requency":30}
The second parameter of json_decode() is assoc (associative). By default it is 0. When it is 0 (default) the json_decode() will return an object, not an array. That's why you are unable to access temp_min by using $json['main']['temp_min'];
However If you use with the value 1 as second parameter, the function will return an array. Parameter 1 means setting associative to 1 (true). So use $json = json_decode($str, true); instead of $json = json_decode($str);. You will be able to access with $json['main']['temp_min']; now.
Also you forgot double quote on line 13 (refresh_requency). Goodluck.
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, true);
$temp = $json['main']['temp_min'];
$content = $temp;
$array = array(
"content" => $content,
"refresh_requency" => 30
);
echo json_encode($array);
Just want help
I change your code like this and that be correctly
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial";
$str = file_get_contents($url);
$json = json_decode($str, TRUE); // Wrong here
$temp = $json['main']['temp_min'];
$content = $temp;
$array = array(
"content" => $content,
"refresh_requency" => 30 // And wrong here, it's must string
);
echo json_encode($array);
?>
More information about json decode in php to array or object http://php.net/manual/en/function.json-decode.php
I am using the following code to pull a CSV file from a website that i do not have control over. and many of times i get the undefined index or headers already sent but all the data is there at the bottom. i want to write a script to open the file and remove all lines until it gets to the actual header line that should be in a csv.
the # of lines changes every time i pull it...
the current example has 49107 lines that i don't need before the part i want to parse.. This is a small part of the first 15 lines of code and about 20 lines of code before what i REALLY WANT from the file.
<pre class="cake-debug"><b>Notice</b> (8): Undefined index: name [<b>APP/controllers/loads_controller.php</b> line <b>327</b>]<div id="cakeErr1-trace" class="cake-stack-trace" style="display: none;">Code | Context<div id="cakeErr1-code" class="cake-code-dump" style="display: none;"><pre><code><span style="color: #000000"> $data[$i]['Load']['drop_date'] = date('m/d/Y' strtotime($value['Load']['drop']));</span></code>
<code><span style="color: #000000"> $data[$i]['Load']['pickup_city'] = $value['Pickup']['city'];</span></code>
"<span class=""code-highlight""><code><span style=""color: #000000""> $data[$i]['Load']['pickup_state'] = $value['Pickup']['State']['name'];</span></code></span></pre></div><pre id=""cakeErr1-context"" class=""cake-context"" style=""display: none;"">$order = ""Load.load_number ASC"""
"$fields = array("
" ""*"""
)
"$conditions = array("
" ""Load.active"" => true"
)
"$results = array("
" array("
" ""Load"" => array()"
" ""Pickup"" => array()"
" ""Destination"" => array()"
)
$result = array(
"Load" => array(
"name" => "ICE CREAM OR RELATED",
"load_number" => "8891517",
"trailer_type" => "R",
"phone_number1" => "800-555-8287",
"phone_number2" => "800-555-8287",
"pickup_date" => "03/09/2014",
"drop_date" => "03/09/2014",
"pickup_city" => "Indianapolis",
"pickup_state" => "Indiana",
"pickup_zipcode" => "46201",
"destination_city" => "London",
"destination_state" => "Kentucky",
"destination_zipcode" => "40741"
)
)
$fp=</pre><pre class="stack-trace">header - [internal], line ??
LoadsController::csv() - APP/controllers/loads_controller.php, line 360
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 170
[main] - APP/webroot/index.php, line 83</pre></div>
</pre>name,load_number,trailer_type,phone_number1,phone_number2,pickup_date,drop_date,pickup_city,pickup_state,pickup_zipcode,destination_city,destination_state,destination_zipcode
"FOOD OR KINDRED PROD",8831029,R,800-555-8287,800-555-8287,03/09/2014,03/10/2014,Aurora,Illinois,60504,"West Memphis",Arkansas,72301
"FOOD OR KINDRED PROD",8831031,R,800-555-8287,800-555-8287,03/12/2014,03/13/2014,Aurora,Illinois,60504,Ashley,Indiana,46705
This is how I would like the file to look after removing the lines that should not be there...
name,load_number,trailer_type,phone_number1,phone_number2,pickup_date,drop_date,pickup_city,pickup_state,pickup_zipcode,destination_city,destination_state,destination_zipcode
FOOD OR KINDRED PROD,8831029,R,800-555-8287,800-555-8287,3/9/2014,3/10/2014,Aurora,Illinois,60504,West Memphis,Arkansas,72301
FOOD OR KINDRED PROD,8831031,R,800-555-5555,800-555-5555,3/12/2014,3/13/2014,Aurora,Illinois,60504,Ashley,Indiana,46705
Currently i am using this code to get my CSV
set_time_limit (24 * 60 * 60);
// folder to save downloaded files to. must end with slash
$destination_folder = 'downloads/';
$url = 'http://www.somesite.com/loads/csv';
$newfname = $destination_folder . 'loads1.csv';
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
and this Code to parse it
$selectfile1 = "https://www.somesite.com/downloads/loads1.csv";
// check mime type - application/octet-stream
$content = file($selectfile1);
$posted_content = array();
list($rownum, $row) = each($content);
$posted_content[0] = explode(",", $row);
array_push($posted_content[0], "ID");
$count = 0;
// iterate each row (1 post)
while (list($rownum, $row) = each($content))
{
$count++;
$cols = "ShipAfterDate, ShipBeforeDate, EquipmentID, LengthID, VendorCode, LoadCount, Rate, CargoDescription, Notes,Phone1, Phone2, PostDate,";
$vals = "";
// extract fields from row columns
$items = explode(",", $row);
list( $Description, $OrderNumber, $EquipmentCode, $Phone1, $Phone2, $ShipDate, $DeliveryDate, $OriginCity, $OriginState, $OriginZip, $DestinationCity, $DestinationState, $DestinationZip
) = $items;
array_push($posted_content, $items);
Check out 'fgetcsv' (PHP manual) which just returns false if there's a parse error or the actual CSV values if not. It might be not the fastest solution to unsuccessfully parse 50k lines, but I think it should work nevertheless
I am trying to pull data out of a CSV file and generate a http_build_query to submit as a http post
My data looks like this:
First,Last,Address,City,St,Zip,email,phone,dob,optindate,ipaddress,url
Abbey,Johnson,4004 S. Parker Dr. 206,Sioux Falls,SD,55106,abbey#email.com,6053451657,06/18/1924,4/19/2008 11:58:34,12.174.252.216,http://www.ecoupons.com/
My code looks like this:
<?PHP
$file_handle = fopen("test.2", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
$data = array('firstname' => "$line_of_text[0]",
'lastname' => "$line_of_text[1]",
'address' => "$line_of_text[2]",);
echo http_build_query($data) . "\n";
}
fclose($file_handle);
?>
My result is:
firstname=Abbey&lastname=Johnson&address=4004+S.+Louise+Ave.+206
firstname=&lastname=&address=
I am not sure why the second line without the data is created and how do I keep the white spaces in the array data?
Thanks!
Your CSV does does seem to be valid or readable (can't see the new line ) so i use a simple of mine
Your address contains , which affects the way fgetcsv reads the file
Try
$fp= fopen("log.txt", "r");
while (!feof($fp) ) {
list($firstname,$lastname,$address) = fgetcsv($fp);
$data = array('firstname' => $firstname,
'lastname' => $lastname,
'address' => $address);
echo http_build_query($data) . PHP_EOL;
}
Output
firstname=Abbey&lastname=Johnso&address=4004+S.+Louise+Ave.+206