Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURL not working when used inside a function

Tags:

php

curl

I'm trying to execute curl using the following code.

mainFunction{
  .
  .
  $url = strtolower($request->get('url', NULL));
  $html_output= $this->startURLCheck($url);
  .
  .
}

function startURLCheck($url)
{
 $ch = curl_init();
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch, CURLOPT_HEADER, false);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
 $html_output = curl_exec($ch);
}

When i give the string URL directly this is working fine. But then I pass the string data through a function curl is not executing. curl_error gives shows no errors too. I tried many encoding and decoding method for the string with same result.Am i doing something wrong? I working using XAMPP server on windows.

I'm passing URL to this function after getting the URL from a HTML post request in another function.

like image 676
Manoj Sreekumar Avatar asked Mar 23 '26 23:03

Manoj Sreekumar


1 Answers

The problem is that your function startURLCheck does not actually return a value for the main program to use. Change the last line:

function startURLCheck($url)
{
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    return curl_exec($ch);
}

In your calling code, take out the "$this->"

$html_output = startURLCheck($url);

$html_output now contains results of the curl call.

I have assumed that you copied and pasted this code from somewhere since your "mainFunction" declaration is syntactically incorrect, and you used "$this->" without specifying that startURLCheck was a method of an object.

If in fact you intend startURLCheck to be an object method and you want it to set $html_output on the object, do this:

<?php

class Example {
    private $html_output;

    function mainFunction()
    { 
        $url='http://www.ebay.com/itm/Apple-iPhone-5-16GB-Black-Slate-Cricket-intl-UNLOCKED-pleeze-read-ad-/251252227033';
        $this->startURLCheck($url);
        echo "HTML output: " . $this->html_output;
    }

    function startURLCheck($url)
    {
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $this->html_output = curl_exec($ch);
    }
}

$example = new Example();
$example->mainFunction();

I have tested this code on the command line (not in a web page). If you copy and paste this into a file and run it using php -r you will see the results. (And note that I didn't include a closing ?> tag. The closing tag is optional when the file contains only PHP code and no HTML. In fact it is recommended that the closing tag be omitted in such cases. See http://php.net/manual/en/language.basic-syntax.instruction-separation.php)

Please also note in your question code for mainFunction you have illegal spaces before "pleeze" in your URL and you are missing the semicolon at the end of the $url assignment.

Hope this helps. Good luck.

like image 106
David Avatar answered Mar 26 '26 13:03

David