Translating Curl or a Wget instruction to php code

Hello,
I received two  urls to receive à .csv file.
I need to do the php code to insert the procedure in a webpage to download a .csv file

Please find the url :
Wget :
wget 'http://LOGIN:PASSWORD@export.mywebsiteurl.be/redirect.jsp?exportFormat=CSV&exportEncoding=utf-8&compressed=1&category=-1'
or
curl -L -u LOGIN:PASSWORD -O 'http://mywebsiteurl.be/redirect.jsp?exportFormat=CSV&exportEncoding=utf-8&compressed=1&category=-1'

Could you help me to make a simple php code.

Regards
Ok123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Of course we cannot test this because we do not have the true URL or the authentication credentials, but I can show you the general design we might use to read from a GET-method resources (like an API).  You might be able to use file_get_contents(), but if the remote resource hangs, file_get_contents() will add the hang time to your script execution time, which can result in a fatal error.  For that reason, I think using cURL is a safer approach to reading from an API, even if it requires more PHP code.

This should be enough for you to get started testing.  Moving parts start around line 80.

<?php // demo/temp_ok123.php

/**
 * http://www.experts-exchange.com/questions/28710179/Translating-Curl-or-a-Wget-instruction-to-php-code.html
 *
 * http://curl.haxx.se/libcurl/c/libcurl-errors.html
 */
error_reporting(E_ALL);

Class Page_Response_Object
{
    public $href, $title, $http_code, $errno, $info, $urls, $document;
    public function __construct($href, $title)
    {
        // AVOID TIMEOUT FOR LONG RUNNING SCRIPT
        set_time_limit(10);

        $this->href  = $href;
        $this->title = $title;
        $this->urls  = [];
        if (!$this->my_curl($href))
        {
            // ACTIVATE THIS TO SEE THE ERRORS AS THEY OCCUR
            // trigger_error("Errno: $this->errno; HTTP: $this->http_code; URL: $this->href", E_USER_WARNING);
        }
        return $this;
    }
    protected function my_curl($url, $timeout=3)
    {
        $curl = curl_init();

        // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
        $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[] = "Cache-Control: max-age=0";
        $header[] = "Connection: keep-alive";
        $header[] = "Keep-Alive: 300";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: en-us,en;q=0.5";
        $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

        // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
        curl_setopt( $curl, CURLOPT_URL,            $url  );
        curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0'  );
        curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
        curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
        curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
        curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
        curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );
        curl_setopt( $curl, CURLOPT_VERBOSE,        TRUE   );
        curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE   );

        // IF USING SSL, THIS INFORMATION MAY BE IMPORTANT
        // http://php.net/manual/en/function.curl-setopt.php#110457
        // http://php.net/manual/en/function.curl-setopt.php#115993
        // http://php.net/manual/en/function.curl-setopt.php#113754
        // REDACTED IN 2015 curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );


        // RUN THE CURL REQUEST AND GET THE RESULTS
        $this->document  = curl_exec($curl);
        $this->errno     = curl_errno($curl);
        $this->info      = curl_getinfo($curl);
        $this->http_code = $this->info['http_code'];
        curl_close($curl);

        // RETURN DOCUMENT SUCCESS SIGNAL
        return $this->document;
    }
}


// DEMONSTRATE THE CLASS IN ACTION
echo '<pre>';

// SET THE LOCATION AND TITLE
$url = 'http://LOGIN:PASSWORD@export.mywebsiteurl.be/redirect.jsp?exportFormat=CSV&exportEncoding=utf-8&compressed=1&category=-1';
$nom = 'Test';

// TRY TO ACQUIRE THE RESOURCE
$pro = new Page_Response_Object($url, $nom);

// IF ERRORS OCCURRED
if ($pro->errno)
{
    echo PHP_EOL;
    echo '<span style="background-color:red;">';
    echo "<b>cURL Errno: $pro->errno, ";
    echo "HTTP Resp: $pro->http_code</b> ";
    echo '</span> ';
}

// SHOW THE WORK PRODUCT
echo $pro->title;
echo PHP_EOL . '<a target="_new" href="' . $pro->href . '">' . $pro->href . '</a>';
echo PHP_EOL;
echo htmlentities($pro->document);

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerCommented:
Ray, he's going to need this, too:

curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);

Open in new window

0
Ray PaseurCommented:
Eddie, good suggestion!  It wasn't clear to me whether the URL would work as written, and without any way to test we're at a bit of a loss.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Ok123Author Commented:
Hi Ray, Hi Eddie,

Thank you for your replies.
I can send you the real url, the login and the password in a private message to test.
I need just to avoid sending that kind of informations in public.

Let me know is it ok for you Ray.
Regards
0
Ray PaseurCommented:
Yes, it is OK, and I'll be glad to help. You can find my email address in my profile.
http://www.experts-exchange.com/members/Ray_Paseur.html
0
Ray PaseurCommented:
I didn't get any message from you.  Please tell us: How did you handle the login and password?  Thanks!
0
Eddie ShipmanAll-around developerCommented:
I think you should have split the point more towards Ray's answer...Ray, I think they can be reassigned, correct?
0
Ray PaseurCommented:
@Eddie: thanks.  Points can be reassigned, but I have plenty of points and don't worry about that stuff any more.  

However I would very much like to see the Author's solution with regard to the credentials!  It seems like something worth adding to my teaching library.
0
Ok123Author Commented:
Hi Ray,

I just sent to you an email because i am traveling right now.
May i ask you how to add your solution in your teaching library as you asked  ?
Regards,
Olivier
0
Ray PaseurCommented:
I keep a teaching library of examples on my own server.  These are often the responses to common questions here at E-E and in classes I teach about PHP.  

The way to present the authentication credentials for the site -- That's what I'm trying to discern.  Did you use CURLOPT_USERPWD or LOGIN:PASSWORD in the URL?  Did either or both work?  Just trying to keep learning :-)

Thanks and regards,
Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.