-1

I am using php Curl for first time This is my code :

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$full = curl_exec($ch);
curl_close($ch);
$full = (string)$full;
$l = strpos($full,'</link>');
$start = strpos($full,'<link>',$l);
$end = strpos($full,'</link>',$l+2);
global $link;
$link = substr($full, $start , $end-$start);
$v = curl_init();
curl_setopt($v,CURLOPT_URL,$link);
curl_setopt($v,CURLOPT_RETURNTRANSFER,true);
$page = curl_exec($v);
curl_close($v);
echo $page;

$ch curl does its job and $link made correctly. now i think my second curl ($v) doesnt work. can anyone help please ?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
Kavian Rabbani
  • 934
  • 5
  • 15

2 Answers2

1

Try this code

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$full = curl_exec($ch);
curl_close($ch);
$full = (string)$full;
$l = strpos($full,'</link>');
$start = strpos($full,'<link>',$l);
$end = strpos($full,'</link>',$l+2);
global $link;
$link = substr($full, $start , $end-$start);
var_dump($link);
$v = curl_init();
curl_setopt($v,CURLOPT_URL,$link);
curl_setopt($v,CURLOPT_RETURNTRANSFER,true);
$page = curl_exec($v);
echo curl_error($v);
curl_close($v);
var_dump($page);

You can see that there a tag in front of your url like

string '<link>http://huger.blog.ir/1394/08/24/start' (length=43)

This is causing the issue

Hari Swaminathan
  • 616
  • 1
  • 13
  • 27
0

In situations like this, if I can get one element to work, I wrap that working element in a function or a class for reuse as is. See if this is what you are looking for:

function cURL($url = false,$settings = false)
    {
        $string =   (!empty($settings['toString']));
        $json   =   (!empty($settings['toJson']));

        $ch     =   curl_init();
        curl_setopt($ch,CURLOPT_URL,'http://huger.blog.ir/rss/');
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $response   =   curl_exec($ch);

        if($string)
            $response   =   (string) $response;
        elseif($json)
            $response   =   json_decode($response,true);

        curl_close($ch);
        return $response;
    }

function toLink($cURL = false)
    {
        $l      =   strpos($cURL,'</link>');
        $start  =   strpos($cURL,'<link>',$l);
        $end    =   strpos($cURL,'</link>',$l+2);
        $link   =   substr($cURL, $start , $end-$start);
        // As noted by @Hari Swaminathan, you have a <link> at the front,
        // so strip_tags will remove that
        return strip_tags($link);
    }

$link   =   cURL('http://huger.blog.ir/rss/',array("toString"=>true));

echo cURL(toLink($link));
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • Hmm, then I am confused as to what you are looking for. Your script above gets a `rss` then strips it out to another url, then tries to reuse it in the `cURL`. That's what this does. You are saying that is not working or that is not what you are trying to do? – Rasclatt Nov 19 '15 at 07:48