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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.