php - Strange Curl behaviour while passing variable

863

I'm using a php Curl function

function file_get_contents_curl($runfile) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_URL,$runfile);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);   
        $content = curl_exec ($ch);

        curl_close ($ch); 
        return $content;
  }

When I call it like

$htmld = file_get_contents_curl('http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567050922&profile=cmpltPrfl&profileId=1921&app=sold');

It helps me to get the page.But when I call it with an array variable like

$htmld = file_get_contents_curl($anchorData[1]);//$anchorData[1] contains the above url.

I'm sure that same url is passed to the method. I have tested with

print_r()

Inside the method. But calling the method with array indexed variable containing same url string doesn't load anything. Any idea about what is the mistake I'm committing? or if possible a little bit explanation will be appreciable. For your information I have first prepared the array by calling the same curl method.

My array data is:

Array ( [0] => http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567050922&profile=cmpltPrfl&profileId=1921&app=sold [1] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567050922&profile=cmpltPrfl&profileId=1921&app=sold' [2] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567128263&profile=cmpltPrfl&profileId=1921&app=sold' [3] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=123456111961&profile=cmpltPrfl&profileId=1921&app=sold' [4] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567132673&profile=cmpltPrfl&profileId=1921&app=sold' [5] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567138184&profile=cmpltPrfl&profileId=1921&app=sold' [6] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567135196&profile=cmpltPrfl&profileId=1921&app=sold' [7] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567045581&profile=cmpltPrfl&profileId=1921&app=sold' [8] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567012024&profile=cmpltPrfl&profileId=1921&app=sold' [9] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567132264&profile=cmpltPrfl&profileId=1921&app=sold' [10] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567008037&profile=cmpltPrfl&profileId=1921&app=sold' [11] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=123456063828&profile=cmpltPrfl&profileId=1921&app=sold' [12] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567028132&profile=cmpltPrfl&profileId=1921&app=sold' [13] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567032740&profile=cmpltPrfl&profileId=1921&app=sold' [14] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567105864&profile=cmpltPrfl&profileId=1921&app=sold' [15] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567028990&profile=cmpltPrfl&profileId=1921&app=sold' [16] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=100204360000&profile=cmpltPrfl&profileId=1921&app=sold' [17] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=123456200387&profile=cmpltPrfl&profileId=1921&app=sold' [18] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=123456167676&profile=cmpltPrfl&profileId=1921&app=sold' [19] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234082710000&profile=cmpltPrfl&profileId=1921&app=sold' [20] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=123456245846&profile=cmpltPrfl&profileId=1921&app=sold' [21] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567055654&profile=cmpltPrfl&profileId=1921&app=sold' [22] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=123456228379&profile=cmpltPrfl&profileId=1921&app=sold' [23] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567093798&profile=cmpltPrfl&profileId=1921&app=sold' [24] => 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=123456254784&profile=cmpltPrfl&profileId=1921&app=sold' ) 
279

Answer

Solution:

$anchorData = array();

    function file_get_contents_curl($runfile) {
       $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $runfile);

        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_URL,$runfile);

        $content = curl_exec ($ch);

        curl_close ($ch); 
        return $content;
    }

    $anchorData[0] = 'http://www.ic.gc.ca/app/ccc/srch/nvgt.do?lang=eng&prtl=1&sbPrtl=&estblmntNo=234567050922&profile=cmpltPrfl&profileId=1921&app=sold';
    $htmld = file_get_contents_curl($anchorData[0]);
    print_r($htmld);

This is the piece of code which i have tested and its running just check it out.

People are also looking for solutions to the problem: javascript - Opening one jquery modal dialog at a time when unable to assign div ids

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.