Get ID from array file_get_contents - parsing

How to get ID's from array to get and put contents from urls. Need get data from url's and save it in folder. I'm new in PHP and try something like this, but it's don't work for me:
$array = array("10", "11", "12");
$contents = file_get_contents('http://www.example.cpm/id='.$array.'&type=text');
file_put_contents('folder/'.$array.'.html', $contents);

Sorry I done it, just using foreach() )))

Related

Rails 5.2.0 —— How to delete sub values of cookies?

I have a cookies.permanent[:liked]:
cookies.permanent[:liked] = 'liked1#liked2#'
I removed liked1#:
cookies.permanent[:liked].slice! `liked1#`
I get cookies.permanent[:liked]:
'liked2#'
Next, I removed 'liked2#':
cookies.permanent[:liked].slice! `liked2#`
and I thought I would get '', but I got:
'liked1#'
And I printed cookies.permanent[:liked], I got 'liked1#liked2#'!
I just want to delete a substring of cookies value, but I find it still can be read.
So, how to do that? Note, I must use permanent.
Yeah, I find I can do this by this:
temp = cookies.permanent[:liked]
wanted_deleted = 'liked1#'
temp.slice! wanted_deleted
cookies.permanent[:liked] = temp
Now, value of cookies.permanent[:liked] is 'liked2#'
There has more efficient ways?

Multiple urls pointing to a single resource with no redundancy

In django's urls.py I got this:
url(r'^main$', 'views.send_partial', name='main'),
url(r'^login$', 'views.send_partial', name='login'),
url(r'^signup$', 'views.send_partial', name='signup'),
url(r'^help$', 'views.send_partial', name='help'),
And I hate repeating code, so I would like to get rid of repeating the same function on and on for every url that should be handled by it. I can not find out how this is done anywhere. So what I am expecting is something like:
url('views.send_partial',
r'^main$', name='main,
r'^login$', name='login',
r'^signup$', name='signup',
r'^help$', name='help')
Ideas?
I have found nothing in the documentation (django.conf.urls), but I think it could be solved with a list/dict of patterns and names.
url_dict = {'main': 'r'^main$', 'login': r'^login$',
'signup': r'^signup$', 'help': r'^help$'}
# This part could also be put into a function taking the
# dictionary and the handler and returning urlpatterns
urls = []
for name, pattern in url_dict.items():
urls.append(url(pattern, 'views.send_partial', name=name))
urlpatterns = patterns('', *urls)
First you create a dictionary mapping the names to patterns (could also be something like a list of lists). Then you loop through the dictionary, creating a list of urlpatterns using url(). Finally you let them trough patterns() or do what else you was doing with them.

My xpath query string doesn´t work

I would like to grab the following value on this website with xquery. After trying for awhile I couldn't figure it out. Here is what I am trying to fetch (image link)
And the code I tried using:
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "???????????????" );
Any ideas?
You can try to grep the value between script tags
Like
//form/div/div/div[#class="values span-7"]/skipt/Text()
After parse the value in PHP other language hat you use to get your string
Can you get the value off of the input?
//form//input[#name="val7"]/#value
Update:
You can get the stuff in the script tag, but that's as far as you can get using xpath. You'll then need to parse the contents of the script tag in order to get that value.
//form[#id="werte"]//div[#class="calval7"]//div[#class="values"][1]/script/text()

YouTube video API not working with IDs beginning with a dash

I am accessing data from YouTube's API, I have everything working fine but the problem I'm having is that when there's a dash (-) at the beginning of the videoID that it's not returning the json data.
$videoID = -FIHqoTcZog;
$json = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$videoID}&alt=json"));
I am however able to return the thumbnail as always with it using this:
$thumbnail = "http://i4.ytimg.com/vi/".$videoID."/mqdefault.jpg";
This is the code that I use to pull the information from the above json that I want.
$title = $json->{'feed'}->{'entry'}[0]->{'title'}->{'$t'};
$description = $json->{'feed'}->{'entry'}[0]->{'media$group'}->{'media$description'}->{'$t'};
$thumbnail = "http://i4.ytimg.com/vi/".$videoID."/mqdefault.jpg";
$ratings = ((round($json->{'feed'}->{'entry'}[0]->{'gd$rating'}->{'average'}, 1)/$json->{'feed'}->{'entry'}[0]->{'gd$rating'}->{'max'})*100)."%";
$views = number_format($json->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'});
$duration = $json->{'feed'}->{'entry'}[0]->{'media$group'}->{'yt$duration'}->{'seconds'};
Are you sure you're only getting a problem with IDs that have a dash in front of it? The code you pasted shouldn't be working with any Youtube ID, because the gdata feed returns, as part of the JSON, some text with the '$' character in it. That character is a PHP identifier, so you'll get 500 errors trying to run the json_decode function on whatever the feed returns.
One way to solve the problem is to use json_decode's 2nd parameter to give you an associative array rather than an object, like this:
$json = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$videoID}&alt=json"),true);
Of course, that requires you to work with an array, too, but the subsequent code changes should be minimal.
If you aren't getting errors with other videos using the exact same code, perhaps you could post it here?

Ruby On Rails: Accessing Array?

I have an array stored in a variable temp which looks like this:
temp.inspect output :
[#"Marlana Letelier", "completed_at"=>nil, "status"=>"incomplete", "name"=>nil, "lead_move_date"=>"2012-06-17 00:00:00", "archive_time"=>nil, "stop_time"=>nil, "priority"=>"2", "assigned_to_user_firstname"=>"Vanessa", "notes"=>"", "created_by_id"=>nil, "id"=>"804005", "assigned_to_id"=>"1", "dn_email_id"=>nil, "outcomes_string"=>"other", "lead_id"=>"101139", "flavor"=>"PhonecallTask", "stringified_parameters"=>"{\n'purpose' => 'continued contact attempt',\n'phone_number' => '361-946-9905',\n}", "created_at"=>"2011-12-21 13:29:07", "start_time"=>"2012-04-04 17:00:00"}>]
temp.class specifies it as an array but temp[1] doesn't output anything.
How do I access the elements ?
EDIT:
1) Temp either had nothing, 1 object or multiple objects
2) Check for nil
3) Get each object out
4) access the attributes
Although your inspect output looks wrong (I think you're missing some text that came out in <...> tags) it looks like you have an array with a single item. Verify this assumption by outputting temp.length.
Since Arrays in Ruby are 0-indexed, try temp[0] or temp.first.

Resources