Extract data from url (PHP) [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
http://accounts.fstatic.org/json/results/?callback=google
I need extract all results from here.
Edit: Extract results by id: example
$result1 = result['1'];
$result2 = result['2'];

Use curl to fetch data and decode the json to array in php.
<?php
$json=fetchPageFromURL('http://accounts.fstatic.org/json/results/?callback=google');
$obj=json_decode($json,true);
print_r($obj);
echo "<hr>";
foreach ($obj as $key => $value) {
foreach (array('result','url','v-url','title','content') as $nnn => $keyname) {
if(0===strpos($key, $keyname)){
$id=substr($key, strlen($keyname), strlen($key)-strlen($keyname));
$fin[$id][$keyname]=$value;
}
}
}
print_r($fin);
function fetchPageFromURL($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
?>
Updated to fetch details and put them away.

$results = json_decode(file_get_contents('http://accounts.fstatic.org/json/results/?callback=google'));

Related

How to validate image from external source [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
User can paste image url on form, then my app download this on local server. I would like validate this image, but i have problem.
Input:
$image = "http://example.com/image.png";
Rules, but not working
'image' => [
'required',
'url',
'mimes:jpeg,png',
'max:5120',
'dimensions:min_width=400,min_height=400',
new FileExtensionRule(['png', 'jpg', 'jpeg'])
]
How can I validate image from external source.
First you have to make custom rule Custom Validation Rules
php artisan make:rule RemoteImage
and then put your logic inside this function in the RemoteImage class
public function passes($attribute, $value)
{
return strtoupper($value) === $value;
}
to get file type
$image = "http://example.com/image.png";
$ext = pathinfo($image, PATHINFO_EXTENSION);
to get the file size to validate
using curl
$ch = curl_init('http://example.com/image.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
echo $size;

Automatically upload table to database [closed]

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 7 years ago.
Improve this question
How can I automatically upload to a database data from tables like the one in this page? I can use their function "export" and then manually download the .csv file, and upload it, but if I want everyday the data from each game, it is a pain... do you think it is possible to automatize it? The only solution would be by scraping their website?
Thanks
You could use the cURL lib of PHP.
Here there's an example:
<?php
$ch = curl_init();
$timeout = 0; // set to zero for no timeout
$url = 'http://www.basketball-reference.com/boxscores/201506160CLE.html'; // set the page url
curl_setopt ($ch, CURLOPT_URL, $url);//enter your url here
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch); //get the page contents
include "simple_html_dom.php";
$table = $file_contents;
$html = str_get_html($table);
$printing = false;
//header('Content-type: application/ms-excel');
$fp = fopen("php://output", "w");
foreach($html->find('tr') as $element){
$arr = array();
foreach ($element->find('tr') as $element2) {
if(!$printing)
$printing = strpos($element2,'Scoring') !== false;
if($printing){
//echo $element2 -> plaintext . "<br>";
$arr[] = $element2 -> plaintext; //comment here
}
}
fputcsv($fp, $arr);
}
fclose($fp);
?>
You have to download a file from here.
You get a text/csv file in your page, if you prefere a plain it's sufficient switch comments (comment line where is requested and decomment the others)
You can parse your new csv as you want

How to fetch information from rest in php? [closed]

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
How to fetch information from rest in php?
I have an url and I wanna extract information from web service by php.
Example of calling GET request
//next example will recieve all messages for specific conversation
$service_url = 'http://example.com/api/conversations/[CONV_CODE]/messages&apikey=[API_KEY]';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
Reference : How to make REST calls in PHP
You can execute curl if is enable on your host.

Website Username Check [closed]

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 1 year ago.
Improve this question
I want to create a function that takes a URL parameter and a username parameter that goes to that url, enters the username and if the page says that the username doesn't exist(error) it will return false, otherwise it will return true. So I tried and I did something a little like this, but it didnt work.
function ($url, $username) {
$main = file_get_contents($url.$username);
if (#$main) { return false; }
else { return true; }
}
So if you have any ideas on how to make this idea actually work please help me
$site = 'https://twitter.com/';
$username = 'SteveMartinToGo';
$url = $site.$username;
function urlExists($url=NULL) {
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode < 400){
return true;
} else {
return false;
}
}
if(urlExists($url)) {
echo 'url exists';
} else {
echo 'url does not exist';
}
The problem with this is that some sites (like Facebook) will return a 200 instead of a 404, so the URL will show as existing even though it's not. Also, I got this function from someplace else (can't remember where though) so I don't want to take credit for that code. hope it helps...
Edit: updated because of fred-ii's eagle eye and suggestions. :)
It is important to have the actual structure of the url. You can make a print and view the source code (HTML) if there is no special characters in the URL can be masked.

PHP code to find time of a city inputted through a input box [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have this code,
<?php
// XML File
$feed_url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=dubai&format=xml&extra=localObsTime&num_of_days=5&key=42esgfa4pfqp6zwgr4egjbph";
// INITIATE CURL.
$curl = curl_init();
// CURL SETTINGS.
curl_setopt($curl, CURLOPT_URL,"$feed_url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
// GRAB THE XML FILE.
$xxml = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($xxml);
$flag=0;
// Loop through ... only pick the first one
foreach ($xml->current_condition as $item) {
if($flag==0){
echo"
Dubai Time And Date: {$item->localObsDateTime}
<br />
";
}
$flag=1;
}
?>
The above code will give me local time and date of the city of Dubai.
I need to take the city value from a text input box and replace "dubai" with that.
This will do the text box part.
I tried to incorporate, but getting some weird time.
<?php
if(isSet($_POST['city']) && $_POST['city'])
{
// XML File
$feed_url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" . urlencode(trim($_POST['city'])) . "&format=xml&extra=localObsTime&num_of_days=5&key=xxxxxxxxx";
// INITIATE CURL.
$curl = curl_init();
// CURL SETTINGS.
curl_setopt($curl, CURLOPT_URL,"$feed_url");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
// GRAB THE XML FILE.
$xxml = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($xxml);
$flag=0;
// Loop through ... only pick the first one
foreach ($xml->current_condition as $item) {
if($flag==0){
echo
ucfirst($_POST['city']) . " Time And Date: {$item->localObsDateTime}
<br />
";
}
$flag=1;
}
}
?>
<form method="post" action="">
<input type="text" name="city" value="<?php echo (isSet($_POST['city']) && $_POST['city'] ? $_POST['city'] : ''); ?>">
<input type="submit" value="Search City">
</form>

Categories