$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));
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 am new to programming,
I need to extract the wikipedia content and put it into html.
//curl request returns json output via json_decode php function
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
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');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$search = $_GET["search"];
if (empty($search)) {
//term param not passed in url
exit;
} else {
//create url to use in curl call
$term = str_replace(" ", "_", $search);
$url = "https://en.wikipedia.org/w/api.php?action=opensearch&search=".$search."&limit=1&namespace=0&format=jsonfm";
$json = curl($url);
$data = json_decode($json, true);
$data = $data['parse']['wikitext']['*'];
}
so I basically want to reprint a wiki page but with my styles and do not know how to do.
Any ideas, Thanks
1)Instead of http://webiste.com/filename i want to take the data from the .txt file which contains 20374 rows and each row contains different website
for example:
website1.com
website2.com
website3.com
etc.
2)parse them individually using curl command
3)get the needed data via preg_match
4)and final results i want to save to my mysql databse
bellow is the code that i am using at the moment, please advise the solution what needs to be added to achieve this goal ?
function curl($url)
{
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: ddosdefend=1d4607e3ac67b865e6c7263260c34e888cae7c56"));
curl_setopt($ch, CURLOPT_USERAGENT, $agent); // The contents of the "User-Agent: " header to be used in a HTTP request.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$test = curl('http://webiste.com/filename');
preg_match('/<iframe class=\"metaframe rptss\" src="(.*?)"/', $test, $matches);
$test2 = $matches[1];
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'));
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.