Jump to content

get_file_contents porting to Curl - Need help


Mad_Coder

Recommended Posts

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.

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

 

 

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.

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);

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>');
*/
?>

 

::) ::)::) 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

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.