Mad_Coder Posted July 6, 2011 Share Posted July 6, 2011 I've been using this script to grab daily devotionals from our affilate for years, then our host disabled get_file_contents, I began porting to curl and it refuses to work and I can not figure out why? Be it... I'm not the best php developer (C# dev by trade) so I'm seeking the help from you pro's. // original get_file_file code <?php // Let's get the content $url = "http://www.streamingfaith.com/prayer/devotionals"; $start_text2 = '<h1 class="dd-title">'; $end_text2 = '</em>'; $source2 = file_get_contents($url); $start_pos2 = strpos($source2, $start_text2) + strlen($start_text2); $end_pos2 = strpos($source2, $end_text2) - $start_pos2; $content = substr($source2, $start_pos2, $end_pos2); // let's strip the content $text = $content; //echo strip_tags($text); echo "\n"; // Let's define Allowed tags and show it all echo strip_tags($text, '<p></p><br/><strong><em>'); ?> // Now here's my attempt to curl - it refuses to strip the tags? <?php $ch = curl_init("http://www.streamingfaith.com/prayer/devotionals"); $source2 = curl_exec($ch); $start_text2 = '<h1 class="dd-title">'; $end_text2 = '</em>'; $start_pos2 = strpos($source2, $start_text2) + strlen($start_text2); $end_pos2 = strpos($source2, $end_text2) - $start_pos2; $content = substr($source2, $start_pos2, $end_pos2); // let's strip the content $text = $content; //echo strip_tags($text); echo "\n"; // Let's define Allowed tags and show it all echo strip_tags($text, '<p></p><br/><strong><em>'); ?> Please help. Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/ Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 So you're getting the contents of the page fine? Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239102 Share on other sites More sharing options...
Mad_Coder Posted July 6, 2011 Author Share Posted July 6, 2011 Yes, the page is returned, however it's not stripping the tags at all. And I can't figure out why. Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239105 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 Echo the raw contents, view the source of that page, post it here. Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239106 Share on other sites More sharing options...
Mad_Coder Posted July 6, 2011 Author Share Posted July 6, 2011 I can't. It exceeds the maximum length here and on phpaste.com. Here it is on my domain though: http://www.thestreamingbible.com/tmp/1.php It will no longer strip the output Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239115 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 What does your script echo? Anything? What content are you trying to extract from that site? Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239121 Share on other sites More sharing options...
Mad_Coder Posted July 6, 2011 Author Share Posted July 6, 2011 If you look above and my curl attempt, I'm trying get the page's content and strip the text only between these tags: $start_text2 = '<h1 class="dd-title">'; // get content starting after this tag $end_text2 = '</em>'; // and ending with this tag Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239122 Share on other sites More sharing options...
Mad_Coder Posted July 6, 2011 Author Share Posted July 6, 2011 I solved the problem. Here's my solution: <?php // Let's get the content $url = "http://www.streamingfaith.com/prayer/devotionals"; $ch = curl_init($url); $source2 = curl_exec($url); $start_text2 = '<h1 class="dd-title">'; $end_text2 = 'Scripture Of The Day:'; $source2 = file_get_contents($url); $start_pos2 = strpos($source2, $start_text2) + strlen($start_text2); $end_pos2 = strpos($source2, $end_text2) - $start_pos2; $content = substr($source2, $start_pos2, $end_pos2); // let's strip the content $text = $content; //echo strip_tags($text); echo "\n"; // Let's define Allowed tags and show it all echo strip_tags($text, '<p></p><br/><strong><em>'); ?> THANKS FOR YOU HELP. Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239126 Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2011 Share Posted July 6, 2011 The curl_exec() function in that is erroring out, and doing nothing, so if file_get_contents() is disabled, how can that possibly work? I'm not a cURL expert, but I think all you needed to do was add the curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); option to the code in the OP, since without it all curl_exec() returns is a 0/1 response. $ch = curl_init("http://www.streamingfaith.com/prayer/devotionals"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $source2 = curl_exec($ch); Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239133 Share on other sites More sharing options...
Mad_Coder Posted July 6, 2011 Author Share Posted July 6, 2011 I know. I totally jumped the gun and it worked locally because get_file_contents is enabled, but when I uploaded it It errored out because I forgot to comment out that function. I will test your code and report back. Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239135 Share on other sites More sharing options...
Mad_Coder Posted July 6, 2011 Author Share Posted July 6, 2011 This doesn't work and I'm at a total loss. I can not for the life of me understand why. <?php // Let's get the content $ch = curl_init("http://www.streamingfaith.com/prayer/devotionals"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $source2 = curl_exec($ch); $start_text2 = '<h1 class="dd-title">'; $end_text2 = 'Scripture Of The Day:'; $start_pos2 = strpos($source2, $start_text2) + strlen($start_text2); $end_pos2 = strpos($source2, $end_text2) - $start_pos2; $content = substr($source2, $start_pos2, $end_pos2); // let's strip the content $text = $content; //echo strip_tags($text); echo "\n"; // Let's define Allowed tags and show it all echo strip_tags($text, '<p></p><br/><strong><em>'); ?> However... I can retrieve the the page's content but it refuses to strip it <?php // Let's get the content $ch = curl_init("http://www.streamingfaith.com/prayer/devotionals"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $source2 = curl_exec($ch); echo $source2; // debug out only /* $start_text2 = '<h1 class="dd-title">'; $end_text2 = 'Scripture Of The Day:'; $start_pos2 = strpos($source2, $start_text2) + strlen($start_text2); $end_pos2 = strpos($source2, $end_text2) - $start_pos2; $content = substr($source2, $start_pos2, $end_pos2); // let's strip the content $text = $content; //echo strip_tags($text); echo "\n"; // Let's define Allowed tags and show it all echo strip_tags($text, '<p></p><br/><strong><em>'); */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239137 Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2011 Share Posted July 6, 2011 That's odd. When I paste your code locally, it seems to work fine. Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239139 Share on other sites More sharing options...
Mad_Coder Posted July 6, 2011 Author Share Posted July 6, 2011 Does it strip the devotional text properly? or just return the entire page? Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239141 Share on other sites More sharing options...
Mad_Coder Posted July 6, 2011 Author Share Posted July 6, 2011 : FINALLY !!! : - WORKS! <?php // Let's get the content $ch = curl_init("http://www.streamingfaith.com/prayer/devotionals"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $source2 = curl_exec($ch); //echo $source2; $start_text2 = '<h1 class="dd-title">'; $end_text2 = 'Scripture Of The Day:'; $start_pos2 = strpos($source2, $start_text2) + strlen($start_text2); $end_pos2 = strpos($source2, $end_text2) - $start_pos2; $content = substr($source2, $start_pos2, $end_pos2); // let's strip the content $text = $content; //echo strip_tags($text); echo "\n"; // Let's define Allowed tags and show it all echo strip_tags($text, '<p></p><br/><strong><em>'); ?> THANKS FOR ALL YOUR HELP - AGIAN Quote Link to comment https://forums.phpfreaks.com/topic/241226-get_file_contents-porting-to-curl-need-help/#findComment-1239143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.