PHP - Scrape Codepad.co Snippet Code

<?php function codepad_co_code( $snippet_id, $header_opts = null ) { $url = "https://codepad.co/snippet/${snippet_id}"; $dom = new DOMDocument(); $dom->preserveWhiteSpace = true; // Change User Agent before request /* $header_opts = array ( 'http' => array ( 'header' => 'User-agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3', ), ); */ if ( ! empty( $header_opts ) && ! empty( $header_opts[ 'http' ] ) ) { $context = stream_context_create( $header_opts ); $result = file_get_contents( $url, false, $context ); @$dom->loadHTML( $result ); } else { // Load URL Directly @$dom->loadHTMLFile( $url ); } // find editor node $xpath = new DOMXpath( $dom ); $result = $xpath->query( '//*[@id="editor"]' ); if ( $result->length > 0 ) { $code = $result->item( 0 )->nodeValue; return $code; } return false; } $code = codepad_co_code( 'd2Rk8KFx' ); echo( '<pre>' . $code . '</pre>' );

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.