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

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.

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

Extract data from url (PHP) [closed]

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'));

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.

How do I use the DailyMotion API? [closed]

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
Can anyone here help me with the integration of the DailyMotion API? I am using their SDK but to no avail, and forums are also not helping much.
I tried using the DailyMotion SDK. It worked very well but later on for some reason I was told not to use SDKs. So here is the PHP code to use the API using cURL.
define("DAILYMOTION_API_KEY", "xyzzz");
define("DAILYMOTION_API_SECRET_KEY", "abcc");
$testUser = "username";
$testPassword = "pwd";
$url = 'https://api.dailymotion.com/oauth/token';
$testVideoFile = "<file location>";
$vidName = "testing video";
$vidDesc = "this is a test";
/* GET ACCESS TOKEN */
try {
$data = "grant_type=password&client_id=" . DAILYMOTION_API_KEY . "&client_secret=" . DAILYMOTION_API_SECRET_KEY . "&username=abs&password=pwd&scope=read+write";
$curlInit = curl_init($url);
curl_setopt($curlInit, CURLOPT_POST, 1);
curl_setopt($curlInit, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curlInit);
curl_close($curlInit);
$res = json_decode($output);
$accessToken = $res->access_token;
$getUploadUrl = "curl -d 'access_token=$accessToken' -G https://api.dailymotion.com/file/upload/";
$uploadUrl = json_decode(system($getUploadUrl));
$postFileCmd = "curl -F 'file=#$testVideoFile'" . ' "' . $uploadUrl->upload_url . '"';
$postFileResponse = json_decode(system($postFileCmd));
$postVideoCmd = "curl -d 'access_token=$accessToken&url=$postFileResponse->url' https://api.dailymotion.com/me/videos";
$postVideoResponse = json_decode(system($postVideoCmd));
$videoId = $postVideoResponse->id;
$publishCmd = "curl -F 'access_token=$accessToken' \
-F 'title=$vidName' \
-F 'published=true' \
-F 'description=this is a test' \
https://api.dailymotion.com/video/$videoId";
$publishres = system($publishCmd);
print_r($publishres);
echo "Video is posted & published Successfully";
} catch (Exception $e) {
print_r($e);
}

Categories