PHP YouTube API Video Title Parsing - php

I'm using some regexes to parse wiki-styled text.
<?php
function wikiParser($data){
$data = preg_replace('/\[\[Youtube:([a-zA-Z0-9_]+)\]\]/', getYoutubeTitle("$1"), $data);
return $data;
}
?>
This function searches for strings like [[Youtube:b32hRITAAew]] and calles another function getYoutubeTitle(b32hRITAAew).
<?php
function getYoutubeTitle($hash){
$url = 'http://gdata.youtube.com/feeds/api/videos?v=2&q='.$hash.'&max-results=1&fields=entry(title)&prettyprint=true';
$fp = fopen($url, 'r');
$page = '';
while(!feof($fp)){
$page .= fgets($fp, 4096);
}
$titre = eregi("<title>(.*)</title>", $page, $regs);
return $regs[1];
}
?>
The second function parses the response data. In the case of b32hRITAAew code, the following url is accessed
http://gdata.youtube.com/feeds/api/videos?v=2&q=b32hRITAAew&max-results=1&fields=entry(title)&prettyprint=true
It outputs:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'>
<entry>
<title>The Lord of the Rings Symphony (1) HQ</title>
</entry>
</feed>
And the title should be The Lord of the Rings Symphony (1) HQ. But for the unknown reason it shows me some random Photography Trick - Easy Image Stabilizer For Any Camera. I've worked hard to solve the issue, but still can't get it how that comes up.
Is there any problem with getYoutubeTitle("$1") or anything else?

You can use the preg_replace_callback function like this:
function wikiParser($data){
$data = preg_replace_callback('/\[\[Youtube:([a-zA-Z0-9_]+)\]\]/', "getYoutubeTitle", $data);
return $data;
}
function getYoutubeTitle($array){
// $array looks like this: Array ( [0] => [[Youtube:b32hRITAAew]] [1] => b32hRITAAew )
$hash = array_pop($array);
$url = 'http://gdata.youtube.com/feeds/api/videos?v=2&q='.$hash.'&max-results=1&fields=entry(title)&prettyprint=true';
...
}

Related

How to download a base64 image in PHP from a web service?

I admit I have a lot of trouble with this problem (I don't know much about PHP and SOAP.
I have to get product pictures for an ecommerce via a web service of a provider (SOAP).
As shown in the picture below, the product pictures are converted to base64.
Example for a product picture
I need to retrieve all the pictures to upload them in a folder on my site and write the path of each picture in a CSV file.
The code below allows me to retrieve all the products that are sent to me via the web service. I recover all the products with the function RecupEtatStock() then, with the help of a loop, for each product I search if there are images with the function RecupPhotoProduct().
$wsdl = "https://sinex-saas.com/B00UWSUG9Z34TN22/Services/WebServiceStandard.asmx?WSDL";
$client = new SoapClient($wsdl);
$password = "????";
$username = "????";
$reffab = 4;
$codefab = 2;
$i = 0;
$fp = fopen(plugin_dir_path(__FILE__).'file.csv', 'w+');
$dataRecupererEtatStock = RecupEtatStock($username, $password, $client);
foreach ($dataRecupererEtatStock as $fields) {
$dataRecupPhotoProduit = RecupPhotoProduit($username, $password, $client, $fields->CodeFab, $fields->RefFab, $i);
$fields->image = $dataRecupPhotoProduit;
fputcsv($fp, get_object_vars($fields));
$i+=1;
}
fclose($fp);
Here is what the RecupPhotoProduct() function looks like. If there is no picture, the function returns null, otherwise it retrieves what the save_image() function returns.
function RecupPhotoProduit($pUsername, $pPassword, $pClient, $pCodeFab, $pReffab, $pi) {
$optionsRecupererPhotoProduit = array(
'user' => $pUsername,
'password' => $pPassword,
'codeFab' => $pCodeFab,
'refFab' => $pReffab,
);
$responseRecupererPhotoProduit = $pClient->RecupererPhotoProduit($optionsRecupererPhotoProduit);
$array = get_object_vars($responseRecupererPhotoProduit);
if(empty($array)) {
$responseRecupererPhotoProduit = null;
} else {
$responseRecupererPhotoProduit = $responseRecupererPhotoProduit->RecupererPhotoProduitResult;
$responseRecupererPhotoProduit = save_image($responseRecupererPhotoProduit, $pi);
}
return $responseRecupererPhotoProduit;
}
The above function allows me to retrieve the string in "RetrievePhotoProductResult" from this XML result:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<RecupererPhotoProduitResponse xmlns="http://tempuri.org/">
<RecupererPhotoProduitResult>/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGB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ep9nEa02LT+JNVFjdG1tIILxZGW28xt8bIMfO+OQeuVHSm3Opajdais4vZFhERiNuwyjM3RyeufTHrVQqM5JA77iSfof6Gp4og3AQH+HZnGfVf6imoRWwilY20dtfWdvbjyovNLiMMcEk5bP+1nmuvt7YSSLCQP3xEYHTIP3sf8B3H8K5W6h1F1d9JTzL5cNCHUHc2cZx6joR7V0/g/QdftH+3eIr6Ke4KnbFHGAFJxzn2GR/wI8VNSooxaE3Y7GiiiuEgKKKKACiiigAooooAKKKKACiiigDlfFaq1xDkZ+Tkeo5rmJFCeYcZKhj83fBA5+oPNFFd9H4EWtiYIDLs5/1rJnPPyrkH6jpn061EVUoG2jmNWx2+Y4I+nf60UVoMQgLkjqN5z7r3/EdasxKvzAqCAyJg9we34dqKKTA19EQRa9tGTmQjLcnhW5+tdjRRXHX+IiW4UUUViIKKKKACiiigD//2Q==</RecupererPhotoProduitResult>
</RecupererPhotoProduitResponse>
</soap:Body>
</soap:Envelope>
So for this example, the MIME64 encoded text is :
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCACEAJoDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+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tjFVZdQNwA1vA03IxJKCIww7gdSfYZpNNgt1lz5Eec4yVz/OteQZXeQCy5KluxxR1Iu1KzMq3UQ3f2q4drhwm7zQmQoP8AdH8IH4k1oXl9b2Fv51xIiKeFywG49hWFf63a6ba2sU++ZpZBDGlvllyeu4jqPXHSsmRLrVLmUam0M0RnDW8YTiNR03HvzzjtW8aPMy5xjeyNfS0m1rV2u76BBHauXh757IfbGGOPdTXU1Q0eDytORyuHnPmtnrz90H3ChR+FX6xm7y0MmFFFFQIKKKKACiiigAooooAK5zV4At+2AMyKHA/Q/wAh+ddHWD4p0a/1fT0GmXiWt5ESUd03KwI6H05wcj0rWjPklcadmcNrcW/WlhcAxS221lJID5J4PseRSW09zZtYrb3Tw2VoCDbquRJGeMMTz8p6fWqkFn4iiviPEgWS7UGNHjUKrp25HXJ6H1FXNvOeOecsevbJ+vQ+hrsVpK5ZcXX9Vjt74taQT3Cu32OJX2h1HRZGPRsZORxVj+3yLm2iFkTbyRlpp9w/cnjaNvVsnPI6YrPCLjjpj3zgf+zL+opGjOfuKfYPxz2+jdvep9nEa02LT+JNVFjdG1tIILxZGW28xt8bIMfO+OQeuVHSm3Opajdais4vZFhERiNuwyjM3RyeufTHrVQqM5JA77iSfof6Gp4og3AQH+HZnGfVf6imoRWwilY20dtfWdvbjyovNLiMMcEk5bP+1nmuvt7YSSLCQP3xEYHTIP3sf8B3H8K5W6h1F1d9JTzL5cNCHUHc2cZx6joR7V0/g/QdftH+3eIr6Ke4KnbFHGAFJxzn2GR/wI8VNSooxaE3Y7GiiiuEgKKKKACiiigAooooAKKKKACiiigDlfFaq1xDkZ+Tkeo5rmJFCeYcZKhj83fBA5+oPNFFd9H4EWtiYIDLs5/1rJnPPyrkH6jpn061EVUoG2jmNWx2+Y4I+nf60UVoMQgLkjqN5z7r3/EdasxKvzAqCAyJg9we34dqKKTA19EQRa9tGTmQjLcnhW5+tdjRRXHX+IiW4UUUViIKKKKACiiigD//2Q==
And this is what my save_image() function looks like. It decodes the base64 image from the "RetrievePhotoProductResult" part of the previous XML and uploads it to my WordPress plugin directory.
function save_image( $base64_img, $title ) {
$base64_img = str_replace('data:image/png;base64,', '', $base64_img);
$base64_img = str_replace(' ', '+', $base64_img);
$data = base64_decode($base64_img);
$file = plugin_dir_path(__FILE__) . $title . '.png';
return file_put_contents($file, $data);
}
But here is my problem, the images download well in my directory. However, I can't open them.
For example, if I try to open them with Paint, this is what I get (Whether I open an image with Paint or not, it's the same thing. I think the problem is the base64 encoding):
Paint cannot read this file. It is not a valid bitmap file or its format is not currently supported.
And if I want to access an image from my ecommerce site, I have a simple white dot that appears:
I have a simple white dot that appears
And here is what the base64 string looks like after decoding:
the base64 string
Do you know how I could download a base64 image via a php web service?
<?php
$encode = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCACEAJoDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU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Glvllyeu4jqPXHSsmRLrVLmUam0M0RnDW8YTiNR03HvzzjtW8aPMy5xjeyNfS0m1rV2u76BBHauXh757IfbGGOPdTXU1Q0eDytORyuHnPmtnrz90H3ChR+FX6xm7y0MmFFFFQIKKKKACiiigAooooAK5zV4At+2AMyKHA/Q/wAh+ddHWD4p0a/1fT0GmXiWt5ESUd03KwI6H05wcj0rWjPklcadmcNrcW/WlhcAxS221lJID5J4PseRSW09zZtYrb3Tw2VoCDbquRJGeMMTz8p6fWqkFn4iiviPEgWS7UGNHjUKrp25HXJ6H1FXNvOeOecsevbJ+vQ+hrsVpK5ZcXX9Vjt74taQT3Cu32OJX2h1HRZGPRsZORxVj+3yLm2iFkTbyRlpp9w/cnjaNvVsnPI6YrPCLjjpj3zgf+zL+opGjOfuKfYPxz2+jdvep9nEa02LT+JNVFjdG1tIILxZGW28xt8bIMfO+OQeuVHSm3Opajdais4vZFhERiNuwyjM3RyeufTHrVQqM5JA77iSfof6Gp4og3AQH+HZnGfVf6imoRWwilY20dtfWdvbjyovNLiMMcEk5bP+1nmuvt7YSSLCQP3xEYHTIP3sf8B3H8K5W6h1F1d9JTzL5cNCHUHc2cZx6joR7V0/g/QdftH+3eIr6Ke4KnbFHGAFJxzn2GR/wI8VNSooxaE3Y7GiiiuEgKKKKACiiigAooooAKKKKACiiigDlfFaq1xDkZ+Tkeo5rmJFCeYcZKhj83fBA5+oPNFFd9H4EWtiYIDLs5/1rJnPPyrkH6jpn061EVUoG2jmNWx2+Y4I+nf60UVoMQgLkjqN5z7r3/EdasxKvzAqCAyJg9we34dqKKTA19EQRa9tGTmQjLcnhW5+tdjRRXHX+IiW4UUUViIKKKKACiiigD//2Q==";
$decode = base64_decode($encode);
$size = getImageSizeFromString($decode);
var_dump($size);
if (empty($size['mime']) || strpos($size['mime'], 'image/') !== 0) {
die('Base64 value is not a valid image');
}
$ext = substr($size['mime'], 6);
$img_file = "filename.{$ext}";
file_put_contents($img_file, $decode);
echo $ext;
?>
this will work for all valid images.

get meta description , title and image from url like facebook link sharing

my code is
function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
else
{
return false;
}
}
function getMetas($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
// preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
preg_match("/<meta name=\"description\" content=\"(.*?)\"/",$str,$title);
// preg_match( '<meta name="description".*content="([^"]+)">siU', $str, $title);
return $title[1];
}
else
{
return false;
}
}
//Example:
$url=$_POST['url'];
echo getTitle($url);
echo "<br><br>";
echo getMetas($url);
this does not shows result for all the url's , example http://google.com
Why are you using regular expression for parsing the <meta> tags ?
PHP has an in-built function for parsing the meta information , it is called the get_meta_tags()
Illustration :
<?php
$tags = get_meta_tags('http://www.stackoverflow.com/');
echo "<pre>";
print_r($tags);
OUTPUT:
Array
(
[twitter:card] => summary
[twitter:domain] => stackoverflow.com
[og:type] => website
[og:image] => http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon#2.png?v=fde65a5a78c6
[og:title] => Stack Overflow
[og:description] => Q&A for professional and enthusiast programmers
[og:url] => http://stackoverflow.com/
)
As you can see the title , image and description are being parsed which you really want.
I know the question is 1.5 years old. But if you are still looking for it, you can use https://urlmeta.org. Its a free API to extract URL meta.
You can check a URL for http or https by
$url='stackoverflow.com';
$http_check='http://';
$https_check='https://';
if(substr($url,0,7)!=$http_check){
$url=$http_check.$url;
}else if(substr($url,0,8)!=$https_check){
$url=$https_check.$url;
}else{
$url=$url
}
then you can use the above answer
<?php
$tags = get_meta_tags($url);
echo "<pre>";
print_r($tags);

How to put XML into my SoapClient Call using a Method

I have generated some XML which is saved to a file.
Dummy_Order.xml
In the following code I wanted to open the XML and use it in the method 'ImpOrders'
Here is my code
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$proxy = new SoapClient('http://soapclient/wsdl/Web?wsdl', array ('trace' => 1));
if (file_exists('Dummy_Order.xml')) {
$xml = file_get_contents('Dummy_Order.xml');
} else {
exit('Failed to open XML.');
}
$xmlstring = new SimpleXMLElement($xml);
$result = $proxy->ImportOrders($xmlstring);
var_dump($result);
echo "REQUEST:\n" . $proxy->__getLastRequest() . "\n";
?>
I am getting a response in $result of 0 imported 0 skipped. So i then did getLastRequest() and it's adding the code from the Method but not adding my XML. It wants my XML in a string - which it current is in and isnt moaning about that (It does moan if i use it ->asXML).
I have tried
$result = $proxy->ImportOrders();
and
$result = $proxy->ImportOrders($xmlstring);
And both show the same result in _getLastRequest, which led me to believe that my string isn't being plugged in.
When I check the functions using _getFunctions it provides the information of this...
ImportResult ImportOrders(string $Orders)
Any help would be awesome!
Ok so i resolved it. All i needed to do was wrap my answer in <<
Like below.
$teststring = <<<XML
$xml
XML;
$result = $proxy->ImportOrders($teststring);
Hope this helps out anyone else using PHP, SoapClient and XML.

Collect Form Data, insert in xml fields to post to Payment Gateway

I need to take payment information from a form and place it in XML and then post it to the payment gateway server. I am rather a novice at best and I am not sure of the easiest way to do this.
I have successfully been able to post to the payment gateway by manually entering the info into the XML on a static php file, so I know the XML is correct, the only real issue for me would be finding the easiest way to take form data and place into the XML.
Below is a sample of the php file and the XML.
<?php
$TransactionId = intval( date(Yms). rand(1,9) . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9). rand(0,9) );
$MerchantId="111111";
$TerminalId="111111";
$ApiPassword="111111";
$private_key="asdfasghgfdhggdfgs";
$ApiPassword_encrypt=hash('sha256',$ApiPassword);
$xmlReq='<?xml version="1.0" encoding="UTF-8" ?>
<TransactionRequest xmlns="https://test.processing.com/securePayments/direct/v1/processor.php">
<Language>ENG</Language>
<Credentials>
<MerchantId>'.$MerchantId.'</MerchantId>
<TerminalId>'.$TerminalId.'</TerminalId>
<TerminalPassword>'.$ApiPassword_encrypt.'</TerminalPassword>
</Credentials>
<TransactionType>LP001</TransactionType>
<TransactionId>'.$TransactionId.'</TransactionId>
<ReturnUrl page="http://www.website.net/response.php">
<Param>
<Key>inv</Key>
<Value>'.$TransactionId.'</Value>
</Param>
</ReturnUrl>
<CurrencyCode>USD</CurrencyCode>
<TotalAmount>44450</TotalAmount>
<CardDetails>
<CardHolderName>John Smith</CardHolderName>
<CardNumber>4653111111111111</CardNumber>
<CardExpireMonth>01</CardExpireMonth>
<CardExpireYear>15</CardExpireYear>
<CardType>VI</CardType>
<CardSecurityCode>030</CardSecurityCode>
<CardIssuingBank>UNKNOWN</CardIssuingBank>
<CardIssueNumber></CardIssueNumber>
</CardDetails>
</TransactionRequest>';
$signature_key=trim($private_key.$ApiPassword.$TransactionId);
$signature=base64_encode(hash_hmac("sha256", trim($xmlReq), $signature_key, True));
$encodedMessage=base64_encode($xmlReq);
markup your static xml with some sort of placeholder like {{key}}. then use this function to fill your xml:
<?php
function fill_template($tpl, $values = array()) {
$find = array();
preg_match_all("/\\{\\{([^}]+)\\}\\}/", $tpl, &$find);
foreach ($find[1] as $x => $key) {
$repl = '';
if (isset($values[$key])) {
$repl = $values[$key];
}
$tpl = str_replace($find[0][$x], $repl);
}
return $tpl;
}
?>
the $values parameter should contain the values from $_POST, along with whatever other values you need to set:
<?php
$payment_map = array('MerchantId' => 'xxx', 'TerminalId' => 'xxx');
$payment_map = array_merge($payment_map, $_POST);
$xml_template = "your markedup xml";
$xml = fill_template($xml_template, $payment_map);
?>
this may not be the most elegant solution but it should get you moving.
(obviously you'll want to validate form data and encode it into proper xml characters if necessary.)

Extracting data from Wikipedia API

I would like to be able to extract a title and description from Wikipedia using json. So... wikipedia isn't my problem, I'm new to json and would like to know how to use it. Now I know there are hundreds of tutorials, but I've been working for hours and it just doesn't display anything, heres my code:
<?php
$url="http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$pageid = $data->query->pageids;
echo $data->query->pages->$pageid->title;
?>
Just so it easier to click:
http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids
I know I've probably just done a tiny thing wrong, but its really bugging me, and the code... I'm used to using xml, and I have pretty much just made the switch, so can you explain it a bit for me and for future visitors, because I'm very confused... Anything you need that I haven't said, just comment it, im sure I can get it, and thanks, in advance!
$pageid was returning an array with one element. If you only want to get the fist one, you should do this:
$pageid = $data->query->pageids[0];
You were probably getting this warning:
Array to string conversion
Full code:
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
echo $data->query->pages->$pageid->title;
I'd do it like this. It supports there being multiple pages in the same call.
$url = "http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$titles = array();
foreach ($data['query']['pages'] as $page) {
$titles[] = $page['title'];
}
var_dump($titles);
/* var_dump returns
array(1) {
[0]=>
string(6) "Google"
}
*/
Try this it will help you 💯%
This code is to extract title and description with the help of Wikipedia api from Wikipedia
<?php
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=extracts|info&exintro&titles=google&format=json&explaintext&redirects&inprop=url&indexpageids';
$json = file_get_contents($url);
$data = json_decode($json);
$pageid = $data->query->pageids[0];
$title = $data->query->pages->$pageid->title;
echo "<b>Title:</b> ".$title."<br>";
$string=$data->query->pages->$pageid->extract;
// to short the length of the string
$description = mb_strimwidth($string, 0, 322, '...');
// if you don't want to trim the text use this
/*
echo "<b>Description:</b> ".$string;
*/
echo "<b>Description:</b> ".$description;
?>

Categories