How to call a website service from PHP?

后端 未结 1 1616
南旧
南旧 2021-01-07 15:46

My problem is the following, I have an EmailReports.php on my server which I use to send mails like EmailReports.php?who=some@gmail.com&what=123456.pdf

I can NOT

1条回答
  •  执笔经年
    2021-01-07 15:59

    You could use a Curl request to retrieve information (xml/html/json/etc) from any website.

    What is CURL? (short answer)

    PHP has a very powerful library of calls that are specifically designed to safely fetch data from remote sites. It's called CURL.

    Source : PHP, CURL, and YOU!

    Example of Curl function in PHP

    /* gets the data from a URL */
    function get_data($url)
    {
     if(function_exists('curl_init')){
     $ch = curl_init();
     $timeout = 5;
     curl_setopt($ch,CURLOPT_URL,$url);
     curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
     curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
     $data = curl_exec($ch);
     curl_close($ch);
     return $data;
     } else 'curl is not available, please install';
     }
    

    Source : Download a URL’s Content Using PHP cURL

    Alternatively, you could do what you are currently doing with file_get_contents but many hosts don't allow this. (Walsh, 2007)

    Usage

    ';
    print_r($mydata); //display the contents in $mydata as preformatted text
    echo '
    '; ?>

    Try to test it, with other websites because more often than not google will return a 404 request (this is to be expected), after a curl has been executed.

    0 讨论(0)
提交回复
热议问题