strange issue with file_get_content for some urls!! - php - php

I have an url and could scrap it using file_get_contents() and collect the links inside :
http://www.140online.com/Class/450/%D8%B3%D9%88%D8%A8%D8%B1%20%D9%85%D8%A7%D8%B1%D9%83%D8%AA
this is the result array :
Array
(
[0] => /company/C79955/المصرية ماركت - امام منصور امام/
[1] => /company/C574901/مريم ماركت/
[2] => /company/C1131/سوبر ماركت اولاد رجب/
[3] => /company/C19699/البركه/
[4] => /company/C21707/سوبر ماركت بست باى/
[5] => /company/C23552/الشامل مى ماركت/
[6] => /company/C21714/سوبر ماركت التميمى/
[7] => /company/C171121/الجزيره/
[8] => /company/C172099/الحمد ماركت/
[9] => /company/C172325/الخشاب ماركت/
[10] => /company/C174805/الدكان ماركت/
[11] => /company/C183996/اسواق الشمس المركزيه/
[12] => /company/C184039/اسواق مرحبا/
[13] => /company/C189580/البحيرى ماركت/
[14] => /company/C216371/سوق زمزم/
[15] => /company/C216941/سيتى ارت/
[16] => /company/C226223/وجينات محمد سعيد محمد موسى/
[17] => /company/C297432/ماركت اولاد محمود/
[18] => /company/C326553/سوبر ماركت عمرو عبد السلام سالم محمد/
[19] => /company/C326566/سوبر ماركت عونى عزيز هنرى/
)
then I need to scrap each link to get some data but here is the problem !
to use any link I have to add the domain name before.
http://www.140online.com/company/C79955/المصرية ماركت - امام منصور امام/
but when I try to get it's contents it send me this error :
file_get_contents(http://www.140online.com/company/C79955/%D8%A7%D9%84%D9%85%D8%B5%D8%B1%D9%8A%D8%A9%20%D9%85%D8%A7%D8%B1%D9%83%D8%AA%20-%20%D8%A7%D9%85%D8%A7%D9%85%20%D9%85%D9%86%D8%B5%D9%88%D8%B1%20%D8%A7%D9%85%D8%A7%D9%85/): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
although I encode the Arabic part as it shown by explode:
$url_ex = explode("/", $itm_url[$i]);
$i_url = 'http://'.$url_ex[2].'/'.$url_ex[3].'/'.$url_ex[4].'/'.rawurlencode($url_ex[5]).'/';
the another strange thing is I can get contents for some links but not for all
please help

I had some problems with file_get_contents for web scraping. I would recommend to use CURL. This library returns HTTP error codes to know what kind of problem you have.
http://php.net/manual/en/book.curl.php
This is an example how to use CURL, I hope it works.
$ch1 = curl_init('string_url');
$options1 = array(CURLOPT_POST => true,
CURLOPT_HEADER => $request_headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTFIELDS => (if u need to send params)
);
curl_setopt_array($ch1, $options1);
$page = curl_exec($ch1);
if(curl_exec($ch1) === false)
{
\print_r(curl_error($ch1));
//HTTP and many other errors if you have it
}
curl_close($ch1);
$dom = new \DOMDocument();
$dom->validateOnParse = true;
$dom->preserveWhiteSpace = false;
$dom->substituteEntities = false;
//Load Document Object Model
$dom->loadHTML($page);
$domxPath = new \DOMXPath($dom);
$urls = $domxPath->query("/html/body//div[#class='urls']");
//Example here searchs all divs which has urls class
//You need to specify which element of DOM contains URLS
//$urls variable must contains an array with urls that you need

Related

Coinmarketcap API call with PHP and choose data set with slug

I tried to do my first API calls which worked finally with help from this great users here in this community. Thanks again. I want to choose data[1] or the currency with symbol. So i could use a $variable from my CMS. Maybe someone can show me a way how i can change this call to symbol. Here is my API call.
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: ___YOUR_API_KEY_HERE___'
];
$request = "{$url}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$json = json_decode($response);
curl_close($curl); // Close request
$price = $json->data[1]->quote->USD->price; echo $price;
You get the data block IDs with array_column(), then you get the symbol's data block ID with array_search():
$data_ids = array_column($json->data, 'symbol');
$symbol_data_id = array_search('ETH', $data_ids);
$price = $json->data[$symbol_data_id]->quote->USD->price;
Or as an oneliner:
$price = $json->data[array_search('ETH', array_column($json->data, 'symbol'))]->quote->USD->price;
LATER UPDATE: OK, let me elaborate on this. Step by step:
You need a proper URL to acces the API. For this you need the API documentation. Your original question mentioned
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
while for your comment question you need something like
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/info?symbol=ETH";
A proper URL will give you a JSON response structured according to its purpose. You have this part ironed out, so I'll not insist on this. From your question text:
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: 1b58ff56-58b2-4fd0-b184-f9c3dd4ff106',
];
$request = "{$url}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, [
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1, // ask for raw response instead of bool
]);
$response = curl_exec($curl); // Send the request, save the response
$json = json_decode($response);
curl_close($curl); // Close request
Then you have to decide how to use the response. For me, the fastest way is to look at its structure (var_dump($json) or print_r($json)). Which will give something like this (the original question):
stdClass Object
(
[status] => stdClass Object
(
[timestamp] => 2021-11-06T18:37:59.447Z
[error_code] => 0
[error_message] =>
[elapsed] => 22
[credit_count] => 1
[notice] =>
[total_count] => 7060
)
[data] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Bitcoin
[...]
)
[1] => stdClass Object
(
[id] => 1027
[name] => Ethereum
[symbol] => ETH
[slug] => ethereum
[...]
[quote] => stdClass Object
(
[USD] => stdClass Object
(
[price] => 4445.0743486785
[volume_24h] => 14137477206.072
[volume_change_24h] => -9.6622
[percent_change_1h] => 0.2898806
[percent_change_24h] => -1.29677209
[percent_change_7d] => 3.13286959
[percent_change_30d] => 23.49191199
[percent_change_60d] => 28.79913805
[percent_change_90d] => 48.37310902
[market_cap] => 525560956659.07
[market_cap_dominance] => 19.5198
[fully_diluted_market_cap] => 525560956659.07
[last_updated] => 2021-11-06T18:37:03.000Z
)
)
)
[2] => stdClass Object [...]
or this (the question in the comment):
stdClass Object
(
[status] => stdClass Object
(
[timestamp] => 2021-11-06T18:03:05.201Z
[error_code] => 0
[error_message] =>
[elapsed] => 12
[credit_count] => 1
[notice] =>
)
[data] => stdClass Object
(
[ETH] => stdClass Object
(
[id] => 1027
[name] => Ethereum
[symbol] => ETH
[category] => coin
[description] => Ethereum (ETH) is a cryptocurrency . Users are able to generate ETH through the process of mining. Ethereum has a current supply of 118,233,336.749. The last known price of Ethereum is 4,424.33123326 USD and is down -1.39 over the last 24 hours. It is currently trading on 4537 active market(s) with $14,138,162,060.93 traded over the last 24 hours. More information can be found at https://www.ethereum.org/.
[...]
[platform] =>
[date_added] => 2015-08-07T00:00:00.000Z
[twitter_username] => ethereum
[is_hidden] => 0
)
)
)
So data is a property of the $json object.
In the first case, data is an array and its structure suggests using array functions to retrieve specific data.
In the second case, data and ETH are objects, while description is a property of ETH. Which allows me to get the description using object syntax
$description = $json->data->ETH->description;

Can not get key value from maxmind return object

I am using maxmind web service to get the user data:
Here is the PHP code to output the data.
Now here you can see I have commented out ip_address, user_type, is_anonymous, is_anonymous_proxy, is_anonymous_vpn, is_tor_exit_node key because I can't get value using those keys?
can you tell me why?
$client = new Client( client number , 'my license keyu');
// Call insights method to get full details
$record = $client->insights( '103.82.11.26' );
//$result['ipAddress'] = $record->traits->ip_address;
$result['country'] = $record->country->name;
$result['city'] = $record->city->name;
//$result['userType'] = $record->traits->user_type;
$result['isp'] = $record->traits->isp;
$result['organization'] = $record->traits->organization;
$result['network'] = $record->traits->network;
//$result['isAanonymous'] = $record->traits->is_anonymous;
//$result['isAnonymous_proxy'] = $record->traits->is_anonymous_proxy;
//$result['isAnonymous_vpn'] = $record->traits->is_anonymous_vpn;
//$result['isTorExitNode'] = $record->traits->is_tor_exit_node;
$result['referer'] = $_SERVER['HTTP_REFERER'];
$result['userAgent'] = $_SERVER['HTTP_USER_AGENT'];
but If I do
print_r( $record );
I can see full object containing all the data.
Updated
Actual Object that is return https://codeshare.io/ad3nVy
you need to use the names as per these keys:
[0] => autonomousSystemNumber
[1] => autonomousSystemOrganization
[2] => connectionType
[3] => domain
[4] => ipAddress
[5] => isAnonymous
[6] => isAnonymousProxy
[7] => isAnonymousVpn
[8] => isHostingProvider
[9] => isLegitimateProxy
[10] => isp
[11] => isPublicProxy
[12] => isSatelliteProvider
[13] => isTorExitNode
[14] => network
[15] => organization
[16] => staticIpScore
[17] => userCount
[18] => userType
)
So, basically for example:
$record->traits->ipAddress

Returning list of users someone is following (over 200)

I've been working on this SoundCloud website for awhile now and have just got into creating a stream page alternative that will display on our website. I have stumbled into an issue however and I'm sure it's an easy solution I'm just now figuring out how 2 achieve the proper output.
$user_ID = $_GET['id'];
$sc = curl_init();
curl_setopt($sc, CURLOPT_URL, 'http://api.soundcloud.com/users/'.$user_ID.'/followings?client_id='.$client_ID.'&page_size=200&cursor=1419617528000');
curl_setopt($sc, CURLOPT_HEADER, 0);
curl_setopt($sc, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($sc);
curl_close($sc);
$content = json_decode($output, true);
As you can see above the code currently is suppose to display up to 400 users in which someone is following, however when we do the following 2 return all of the content which is being displayed on that link.
echo "<pre>";
print_r($content);
echo "</pre>";
It appears that it caps at only 200 results ( which is really not that good we need 2 find an alternative 2 display the full amount of users someone is following. ) at the bottom of th page it displays another message.
[199] => Array
(
[avatar_url] => http://a1.sndcdn.com/images/default_avatar_large.png?1485508269
[id] => 72067899
[kind] => user
[permalink_url] => http://soundcloud.com/berthalie30
[uri] => https://api.soundcloud.com/users/72067899
[username] => berthalie30
[permalink] => berthalie30
[last_modified] => 2016/04/06 21:53:03 +0000
[first_name] =>
[last_name] =>
[full_name] =>
[city] =>
[description] =>
[country] =>
[track_count] => 0
[public_favorites_count] => 0
[followers_count] => 21
[followings_count] => 89
[plan] => Free
[myspace_name] =>
[discogs_name] =>
[website_title] =>
[website] =>
[reposts_count] => 32
[comments_count] => 0
[online] =>
[likes_count] => 0
[playlist_count] => 6
)
)
[next_href] => https://api.soundcloud.com/users/24537746/followings?client_id=XXXX&page_size=200&cursor=1406526915000
However, even using this method does nothing (even though I'm more then certain the user I am testing with has more then 200 users he's following.)
EDIT : Someone here is having the same issue, how ever I don't understand the comment solution on how 2 prevent it from happening..

Last-Modified to check feed is updated since last download

i am using PHP to read feed xml in every half an hour, as of now i am always reading whole feed file whether it is updated or not. But i want to check whether feed xml is update since last download, if updated then only read the xml otherwise not.
I am trying to achieve it using below code, but $lastmodifiedRSS is always empty. I am not sure what is going wrong in my code. If i get $lastmodifiedRSS then i can easily compare it with last loaded time and then decide what to do.
If any Expert can share some info that would be great. Thank you in advance.
//get feed information & content
$feed = new feed($rssurl);
$feed->load();
//get the last modified date (web server information)
$lastmodifiedRSS = $feed->http->get_header_value('Last-Modified');
function http($url)
{
$this->url($url);
$this->user_agent = 'posh';
$this->header = "";
$this->code = "";
$this->codeStr = "";
$this->max_d = 5;
$this->authorization = "";
$this->proxy_auth = "";
$this->url = $url;
$this->addProxy();
}
function feed($url)
{
if ($url) {
$this->url = $url;
$this->http = new http($url);
//$this->addProxy();
} else {
$this->http = new http('');
}
}
function load()
{
$content= $this->http->get();
if (!$content)
return false;
$content = $this->transcode($content);
return $content;
}
function get_header_value($name)
{
if (preg_match("/$name: ?([^\\r\\n]*)\\r\\n/m",$this->head,$matches) !== false)
{
if (count($matches) > 1)
{
return $matches[1];
}
}
return false;
}
Regards,
Mona
Make use of stat() in PHP
<?php
print_r(stat('users.json'));
OUTPUT:
Array ( [0] => 2 [1] => 0 [2] => 33206 [3] => 1 [4] => 0 [5] => 0 [6] => 2 [7] => 298 [8] => 1384073940 [9] => 1384073940 [10] => 1368626190 [11] => -1 [12] => -1 [dev] => 2 [ino] => 0 [mode] => 33206 [nlink] => 1 [uid] => 0 [gid] => 0 [rdev] => 2 [size] => 298 [atime] => 1384073940 [mtime] => 1384073940 [ctime] => 1368626190 [blksize] => -1 [blocks] => -1 )
Source
Keeping a track of the variables [atime] , [size] can help you achieve what you are trying to do.
This kind of checking are better handled with HTTP headers itself.
If the server sends Etag header in the response, you could use it with the If-None-Match header on the next request to know if the file has changed.
Similarly, if the server sends Last-Modified header in the response, you could use this timestamp with the If-Modified-Since header on the next request to know if the file got changed.
Just to let you know what you could do, I can't contribute any code cause I don't know which HTTP library you're using here.

php , SimpleXML, extract info from array

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;

Categories