Jump to content

html headers with discogs api to remove bbcode


Recommended Posts

i looked everywhere but couldnt find a solution... how do i parse discogs bbcode and convert it to html, or just strip it out ?


api is returning band biographys with lot of non-standard bbcode and discogs internal link... how can i parse it to human-readable text ready to be displayed to end user on my website ?


i asked in discogs forum and got this answer:


If you don't want that specific format, you can include an Accept header in your response with your preferred response type. We currently offer Discogs format (what you've got now), plaintext (URLs and other formatting is just stripped away), and HTML. For more information, see the API docs: https://www.discogs.com/developers/#page:home,header:home-versioning-and-media-types


 


So i tried this but it didn't work:


curl_setopt($ch, CURLOPT_HTTPHEADER, array('application/vnd.discogs.v2.html+json'));

 


here's what i have so far



$ch = curl_init(); //initialize the session
curl_setopt($ch, CURLOPT_USERAGENT, 'APN/1.0 +https://www.anarcho-punk.net'); //Set the User-Agent Identifier
curl_setopt($ch, CURLOPT_URL, $discogs_label_apiurl); //Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Ask cURL to return the contents in a variable instead of simply echoing them
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('application/vnd.discogs.v2.html+json'));
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('ACCEPT' => 'application/vnd.discogs.v2.html+json'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('ACCEPT' => 'text/html'));
$output = curl_exec($ch);

Link to comment
Share on other sites

Can you elaborate ? Just linking the manual with so many different parameters isn't helping me :(

CURLOPT_HTTPHEADER	An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')

So my problem is that i didn't set the content-length ? Or i don't need the "ACCEPT" Part ? Or is it something else ?

Link to comment
Share on other sites

Dude. How hard can it be to read a string which is literally on the screen right in front of you?

Content-type: text/plain

Let's do it together: C - o - n - t - e - n - t - hyphen - t - y - p - e - colon - t - e - x - t - slash - p - l - a - i - n.

 

Or more general: Header name - colon - header value. Name, colon, value. Name, colon, value. Really simply.

 

Stackoverflow cannot do the reading for you either. In fact, you would have -10 votes and a closed topic by now.

Link to comment
Share on other sites

I answer a good number of questions over on SO (top 4% overall at the moment), and lately I have noticed that most PHP questions get closed as "Duplicate" in record time. Often within a minute of the question being asked. Unless it's very clear and complete, you're going to get shutdown, and even if you've got a complete, verifiable question, with working code and example data, you still often get marked as dupe, even if there are subtle or even important distinctions in what your question actually entails. In your case, there are numerous CURL related questions that have already been answered, so if you're expecting detailed hand holding, which is not guaranteed but often happens here, you are unlikely to find it.

 

Your question is missing a key ingredient: The specific url you are requesting. If this is not accessible to the public for test purposes, you can always grab the json body of the response and stick it here or in one of the fiddles around.

 

It seems to me that given the simplicity of the default json response, and the usefulness of json_decode(), you have been steered down the wrong path here, perhaps given your original question.

 

Rather than jump to the conclusion of: "Give me something different" it would be helpful if you had shown us what you got, and explained in detail why you were specifically having problems with it.

 

Just because you ask for text, doesn't mean that the text won't have html tags etc in it. I also question that you ever got bbcode, but again your question is missing a lot of details that would have been helpful and might have gotten you a response you would have found more helpful.

Link to comment
Share on other sites

In case the problem still isn't clear: He's using the wrong format for setting the Accept header, so he doesn't get the response type he asked for.

 

The solution is simply to use the right format. I've pointed him to that information, he has even quoted the exact line from the manual, but for some reason he cannot see what's in front of him.

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/vnd.discogs.v2.html+json']);    // the right format: header name, colon, header value

Ironically, not a single user on his beloved Stackoverflow (where he asked the same question) managed to get that right. So whatever he's looking for, it's not competence.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.