php , SimpleXML, extract info from array - php

I'm trying to extract some information from ebay api . I have this link up http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=Morcovar-c74b-47c0-954f-463afb69a4b3&siteid=0&version=525&IncludeSelector=ItemSpecifics&ItemID=220617293997,250645537939,230485306218 and I'm using SimpleXML . So far I managed to extract all the info that I needed except the "PictureURL". The issue is that when I make a print to the simpleXMl it appears that the pictures are in an array
[GalleryURL] => http://thumbs4.ebaystatic.com/pict/1105372138158080_1.jpg
[PictureURL] => Array
(
[0] => http://i.ebayimg.com/09/!Bt+mKZQCWk~$(KGrHqYH-CYEvrwcUC47BL-,(K(pnw~~_1.JPG?set_id=8800005007
[1] => http://i.ebayimg.com/01/!Bt+mDTwB2k~$(KGrHqUH-DEEvirheBYUBL-,WtytJQ~~_1.JPG?set_id=8800005007
[2] => http://i.ebayimg.com/22/!Bt+mE8!EGk~$(KGrHqUH-CMEvsjKcE3JBL-,Wzr+sw~~_1.JPG?set_id=8800005007
[3] => http://i.ebayimg.com/17/!Bt+mFg!EGk~$(KGrHqIH-DoEvp43,)33BL-,W14vYQ~~_1.JPG?set_id=8800005007
[4] => http://i.ebayimg.com/01/!Bt+mQ0!!2k~$(KGrHqIH-EQEvqDDLQZVBL-,(j1YGg~~_1.JPG?set_id=8800005007
[5] => http://i.ebayimg.com/01/!Bt+mSq!EGk~$(KGrHqQH-C4Evs(Rz(JWBL-,(rdtsw~~_1.JPG?set_id=8800005007
[6] => http://i.ebayimg.com/03/!Bt+mUBw!Wk~$(KGrHqEH-DEEvnQtM9VkBL-,(w1+lQ~~_1.JPG?set_id=8800005007
[7] => http://i.ebayimg.com/15/!Bt+mHKQEGk~$(KGrHqQH-E!Evlr98iwBBL-,W87Nug~~_1.JPG?set_id=8800005007
[8] => http://i.ebayimg.com/13/!Bt+mI3!Bmk~$(KGrHqMH-DkEvq1,F2ooBL-,(EQ7Vg~~_1.JPG?set_id=8800005007
[9] => http://i.ebayimg.com/05/!Bt+mL2gEWk~$(KGrHqIH-EYEvov7vQY4BL-,(PzCKQ~~_1.JPG?set_id=8800005007
[10] => http://i.ebayimg.com/24/!Bt+mNlwEWk~$(KGrHqIH-CYEvqPjh6RQBL-,(Wh+S!~~_1.JPG?set_id=8800005007
[11] => http://i.ebayimg.com/19/!Bt+mPE!!2k~$(KGrHqQH-CYEvr5z9)NVBL-,(c3bzw~~_1.JPG?set_id=8800005007
)
But if I try to get them from the array I get no result . Here is the code that I currently have
$items = "220617293997,250645537939,230485306218,110537213815,180519294810";
$number_of_items = count(explode(",", $items));
$xml = $baseClass->getContent("http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=Morcovar-c74b-47c0-954f-463afb69a4b3&siteid=0&version=525&IncludeSelector=ItemSpecifics,description&ItemID=$items");
writeDoc($xml, "api.xml");
$getvalues = simplexml_load_file('api.xml');
$picture_url = $getvalue->Item[$number]->PictureURL[2];
echo "picture url is $picture_url";
Using the above code I was expecting to extract the 2nd picture url from the array .
thank you in advance for any help !

Can't test this, I'm afraid, but have you tried var_dump($picture_url) to see what $picture_url is? Maybe it needs to be cast to string?
echo "picture url is" . (string)$picture_url;

Related

PHP - strpos on array elements?

For some reason I can't get strpos to work to search my array, even if $jobList[1] and $titlesearch are the same values... Sorry if it's something obvious but I'm still pretty new to coding!
I begin with my $data array which looks like this:
Array
(
[0] => P0001 Lifeguard descexample 18/09/18 parttime fixedterm mail vic
[2] => P0002 IT Manager descexample 18/09/18 fulltime ongoing post mail sa
)
I then explode each of these entries into their own array...
for ($i = 0; $i < count($data); $i++) {
$jobList = explode("\t", $data[$i]);
}
Array
(
[0] => P0001
[1] => Lifeguard
[2] => descexample
[3] => 18/09/18
[4] => parttime
[5] => fixedterm
[6] =>
[7] => mail
[8] => vic
)
Array
(
[0] => P0002
[1] => IT Manager
[2] => descexample
[3] => 18/09/18
[4] => fulltime
[5] => ongoing
[6] => post
[7] => mail
[8] => sa
)
Now I'm trying to search through these arrays from a user input, $titlesearch, and find it's matches with the job titles, $jobList[1]:
if (strpos($jobList[1], $titlesearch)) {
echo "nice one";
}
No matter what loops I try, the strpos never returns true, even if I echo the values and they both give the same result, so I'm really not sure what I'm doing wrong :'(
Any help is greatly appreciated!
You should always compare the data type when using this function as it may not return a boolean and it can be missleading. Check documentation here
Try it something like this:
if (strpos($jobList[1], $titlesearch) !== false) {
echo "nice one";
}

Get value of Last slash and before . in php [duplicate]

This question already has answers here:
How can I get the name of the image from url?
(8 answers)
Closed 5 years ago.
I have array like below
Array
(
[0] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg
[1] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg
[2] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR06A-961000001.jpg
[3] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR06A-96529.jpg
[4] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR07A-971000002.jpg
[5] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR07A-97530.jpg
[6] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR08A-981000003.jpg
[7] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR08A-98531.jpg
[8] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-991000004.jpg
[9] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99532.jpg
[10] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99533.jpg
[11] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99534.jpg
[12] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno53.jpg
[13] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno54.jpg
[14] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno55.jpg
[15] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno56.jpg
[16] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno57.jpg
)
I want to get value after last slash and before .jpg like SyR04A-94527,SyR05A-95528 etc..
You're trying to parse filenames, and PHP already has plenty of built-in functions to do that. There's no need to manipulate the string itself with explode or regular expressions, etc:
$filename = 'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg';
echo pathinfo($filename, PATHINFO_FILENAME);
// SyR04A-94527
Using Regx:
<?php
$a = [
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg',
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg'
];
foreach( $a AS $path ){
if( preg_match('/\/([^\/]+)\.[a-z]+$/i', $path, $match))
print_r($match[1]."\n");
}
Outputs:
SyR04A-94527
SyR05A-95528
You can test it here http://sandbox.onlinephpfunctions.com/code/081543329dda8b9e0ef59836995184171ff4ce66
I was going to use pathinfo but my sandbox site has it disabled, and I'm to lazy to turn my server on. But it would be something like this:
$a = [
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg',
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg'
];
foreach( $a AS $path )
echo pathinfo ($path, PATHINFO_FILENAME)."\n";
Cheers!

Function returns array - PHP

I have this class:
class Dados{
function cadastro($store){
preg_match_all('#<div align=\'left\'>(.*?)</div>#s',$store,$dados);
return $dados[1];
}}
And this is the output of print_r($dados->cadastro($pagina_cadastro));
Array
(
[0] => 02A
[1] => M
[2] => 72 - CIÊNCIA DA COMPUTAÇÃO/N
[3] => 1402
[4] => 23/12/2013
[5] => 6/7/1995
[6] => S
[7] => M
[8] => Details Removed
[9] => Details Removed
[10] => Details Removed
[11] => Details Removed
[12] => Details Removed
[13] => Details Removed
[14] => My Email
[15] => Details Removed
[16] =>
[17] => Details Removed
[18] => Details Removed
)
Details were removed to protect my privacy
So the question is, how can use echo to print just positions 3, 7 and 9 (for example) ?
I already try $dados->cadastro[1]($pagina_cadastro); and $dados->cadastro($pagina_cadastro)[1]; but any works.
I think it's a newbie question but I really need help.
Thank you so much!
Assign the result to a variable first:
$details = $dados->cadastro($pagina_cadastro);
and then you can get any position you want by:
echo $details[3];
echo $details[7];
echo $details[9];
may be try like this:
$val = $dados->cadastro($pagina_cadastro);
echo $val[3];// similarly other also.
if this
<?php
class Dados{
function cadastro($store){
preg_match_all('#<div align=\'left\'>(.*?)</div>#s',$store,$dados);
return $dados[1];
}
}
$var = new Dados();
print_r(var->cadastro());
?>
returns your array.
Then you do like this
<?php
//Previous code before this
$array = var->cadastro();
echo $array[7] . $array[9]; //etc
?>
To specify specific values
Just assign your function call to a variable like this:
$var = $dados->cadastro($pagina_cadastro);
Then you can print any index of the array like this :
print $var[3];
print $var[7];
print $var[9];

get instagram user id using php foreach in wordpress

I created wordpress shortcode to get user ID by search,
I using foreach() but result not working!
I want foreach because i will make shortocde to get latest instagram images,
Resource:
http://instagram.com/developer/endpoints/users/
http://codex.wordpress.org/Function_Reference/wp_remote_get
http://codex.wordpress.org/Function_Reference/wp_remote_retrieve_body
This is my code:
function insta_id($atts, $content = null){
$my_access_token = "MY ACCESS TOKEN IS HERE";
$get_id = wp_remote_get("https://api.instagram.com/v1/users/search?q=youtube&access_token=$my_access_token&count=2");
$retrieve_id = wp_remote_retrieve_body( $get_id );
$result = json_decode($retrieve_id, true);
foreach ( $result as $user_id ) { // loop start
print_r($user_id); // this working but display all array
echo $user_id->id; // not working!
}
}
add_shortcode("insta_id", "insta_id");
print_r($user_id); display this:
Array ( [0] => Array ( [username] => youtube [bio] => Behind the scenes with stars from your favorite YouTube channels. [website] => [profile_picture] => http://photos-b.ak.instagram.com/hphotos-ak-xaf1/10691785_700124036737985_752862120_a.jpg [full_name] => [id] => 1337343 ) [1] => Array ( [username] => youtubewtfff [bio] => ғυnniest moments on yoυтυвe 😂 Kik: Youtubewtf Email : tysroark#hotmail.com Want a shoutout? Tag #Youtubewtf [website] => [profile_picture] => http://images.ak.instagram.com/profiles/profile_489772119_75sq_1391400797.jpg [full_name] =>
You may be retrieving the wrong key from the response. Double check the instagram User ID with https://www.thekeygram.com/find-instagram-user-id/
It should be: $user_id['id']

sort files in a directory based on partial file name in php

I am using glob() php function to find the files in a directory.
I am getting the following array as result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[6] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-5.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
)
What I want is "To sort the files on the basis of partial text of file names". As in the above array, when you may realize I was supposed to expect the result to be sorted as the ...-0.jpeg, ...-1.jpeg, ...-2.jpeg not ...-0.jpeg, ...-1.jpeg, ...-10.jpeg.
I am expecting the following result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
)
Kindly help, how I can do it using PHP with/without regular expression?
Use natsort function that sort an array using a "natural order" algorithm
As you have all strings in an array, simply use php's built-in function sort().
It would work like this:
sort($array);
Since all files are in the same directory, this should solve the incorrect order you have. If it doesn't, you can also try this:
//Get rid of directory
foreach($array as $key=>$value){
$value=str_replace('D:\xampp\htdocs\myproject\app\webroot\batch\temp\\','',$value); //Watch out, the last backslash has to be escaped with another backslash, otherwise the string will not end
}
//Sort only the filenames
$sort($array);
//Prepend directory again
foreach($array as $key=>$value){
$value='D:\xampp\htdocs\myproject\app\webroot\batch\temp\\'.$value;
}

Categories