I'm using the following code with cURL:
function _getStatsDataXMLString($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 (.NET CLR 3.5.30729)');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
$xmlstr = curl_exec($curl);
curl_close($curl);
return $xmlstr;
}
The site I am trying to grab from is:
http://xml.heroesofnewerth.com/xml_requester.php?f=player_stats&opt=nick&nick[]=yanagafol
Whenever I try to request data with the above function, the string it generates is:
yanagafol173957844341541169317315000150000.009142347141111962425534613119823402602572171277264755751031151494960762160734306050303782332556000164255279111749522139194314968322760755616358407012000000011
I get the same printout with print_r
It is spaced out properly, but doesn't have any of the xml tag information so I can't convert it into an object for use with my database.
Any help would be greatly appreciated.
Try using htmlspecialchars to encode special characters in the string so that they print out as literals:
$xmlstr = htmlspecialchars(curl_exec($curl));
The inverse is htmlspecialchars_decode and htmlentities gives you more control over what gets encoded.
Related
I'm new to php and curl. I'm calling a webservice that returns values in headers and a json object.
The return string is ONLY has the json object, no information about it, so it looks like
[{
"time":"2017-07-18T17:02:57.759Z",
"trade_id":18237183,
"price":"2302.53000000",
"size":"0.03310247",
"side":"sell"
}]
From my understanding the string should have the return headers and other information. (or do I not understand this)
code:
create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "https://api.gdax.com/products/BTC-USD/trades");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
// set user agent
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string
$output = curl_exec($ch);
// ONLY DISPLAYS JSON OBJECT NO INFORMTION ON TOP OF STRING
echo( $output);
// close curl resource to free up system resources
curl_close($ch);
I have an extremely simple script:
<?php
$jsonurl = "http://api.wipmania.com/json";
$json = file_get_contents($jsonurl);
echo $json;
?>
It works for this URL, but when I call it with this URL: https://erikberg.com/nba/standings.json
it is not echoing the data. What is the reason for this? I'm probably missing a concept here. Thanks
The problem for that particular URL is that it's expecting a different User Agent, different to the default that PHP is using with file_get_contents()
Here is a better example using CURL. It's more robust although it takes more lines of code to configure it and make it run:
// create curl resource
$ch = curl_init();
// set the URL
curl_setopt($ch, CURLOPT_URL, 'https://erikberg.com/nba/standings.json');
// Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Fake the User Agent for this particular API endpoint
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
// $output contains the output string.
$output = curl_exec($ch);
// close curl resource to free up system resources.
curl_close($ch);
// You have your JSON response here
echo $output;
I want get the share count on my Viadeo link like I can do with other social API with a json file.
I have tried to do that: https://api.viadeo.com/recommend?url=http://myURL.dev but it's doesnt work. It give me some informations but not the share count.
Someone have an idea for help me to get it?
Of course it won't work on a dummy url. You should provide a valid one first. It seems it return a JSON. Just use a simple json_decode(). Example:
$url = 'https://api.viadeo.com/recommend?url=http://us.viadeo.com/en/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$results = curl_exec($ch);
$data = json_decode($results, true);
echo $data['count']; // for this example it should show
// 12
I have used the wrong item. It's work now.
I have little function for get content result with cURL;
When i tried without POST, all working good, but with POST enabled, the result is null.
This code don't work:
function getAPI($url){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,'shard=Apex');
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3");
$result = curl_exec($ch);
return $result;
curl_close($ch);
}
This code work but the request is not passed like a POST:
function getAPI($url){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
//curl_setopt($ch,CURLOPT_POST, true);
//curl_setopt($ch,CURLOPT_POSTFIELDS,'shard=Apex');
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3");
$result = curl_exec($ch);
return $result;
curl_close($ch);
}
I call this function with this command:
echo getAPI('http://world.needforspeed.com/SpeedAPI/ws/cmsbridge/news/rss/fr_FR');
You can also try with this URL
http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/driver/levelkro/profile
This URL work only if shard=Apex (default shard=CHICANE for CHHICANE Server, but my profil is only available on Apex Server)
According to the SpeedAPI-manual (a simple Google-search) it would seem that the given URL does not accept the "shard" POST-parameter...
SpeedAPI Manual for Get News RSS Feed
You can however use a GET-request and use a shard-parameter where applicable according to the manual.
For example: http://world.needforspeed.com/SpeedAPI/ws/game/nfsw/driver/DRIVER_NAME/profile?shard=APEX (make sure you enter the shard-parameter in capitals)
Have you tried passing the parameter as an array like this instead:
curl_setopt($ch,CURLOPT_POSTFIELDS,array('shard' => 'Apex'));
$url = "https://graph.facebook.com/fql?q=SELECT%20uid2%20FROM%20friend%20WHERE%20uid1=me()&access_token=sadfasdf";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$string = curl_exec($ch); // grab URL and pass it to the browser
echo $string;
curl_close($ch);
Right now, echo $string outputs the entire data grabbed as plain text.
{ "data": [
{ "uid2": "91298391" },
{ "uid2": "00291509" },
{ "uid2": "101927261" }
]}
How can I instead grab specific data similar to JSON? In this case, I want to be able to grab every id. I have a feeling I will use preg_match but I'm stumped.
php has a built in function for turning a json string into an array.
json_decode
the second parameter makes it give you back an assoc array instead of an object. Then just grab the values:
$ids = array_values(json_decode($string, true));