繁体   English   中英

php脚本在浏览器中运行时有效,但在运行cron事件时则无效

[英]php script works when running in browser but doesn't when running cron event

我有一个php脚本页面,该页面使用一些Vebra API将属性信息存储到MySQL数据库中。 我目前有for循环,您需要首先获取XML的分支信息,然后从中使用url以XML的形式获取每个分支的属性ID,然后使用也以XML排序的每个属性ID的URL请求属性信息。

这样循环,然后将所有信息添加到MySQL中。 所有这些在浏览器中都可以正常工作,但是当以cron的身份运行时却无法完成。

连接脚本如下。 在实时版本中,它具有带datafeedid的用户名和密码。 然后,我在底部运行connect函数,然后对结果进行循环。

session_start();

            //Define Your Unique Variable Here:
            $username = "";
            $password = "";
            $datafeedID = "";
            $request = "http://webservices.vebra.com/export/$datafeedID/v1/branch";

            //Function to authenticate self to API and return/store the Token
            function getToken($url) {

                //Re-define username and password variable scope so they can be used within the function
                global $username, $password;

                //Overwiting the response headers from each attempt in this file (for information only)
                $file = "headers.txt";
                $fh = fopen($file, "w");

                //Start curl session
                $ch = curl_init($url);
                //Define Basic HTTP Authentication method
                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                //Provide Username and Password Details
                curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
                //Show headers in returned data but not body as we are only using this curl session to aquire and store the token
                curl_setopt($ch, CURLOPT_HEADER, 1); 
                curl_setopt($ch, CURLOPT_NOBODY, 1); 
                //write the output (returned headers) to file
                curl_setopt($ch, CURLOPT_FILE, $fh);
                //execute curl session
                curl_exec($ch);
                // close curl session
                curl_close($ch); 
                //close headers.txt file
                fclose($fh); 

                //read each line of the returned headers back into an array
                $headers = file('headers.txt', FILE_SKIP_EMPTY_LINES);

                //for each line of the array explode the line by ':' (Seperating the header name from its value)
                foreach ($headers as $headerLine) {

                    $line = explode(':', $headerLine);
                    $header = $line[0];
                    $value = trim($line[1]);

                    //If the request is successful and we are returned a token
                    if($header == "Token") {
                            //save token start and expire time (roughly)
                            $tokenStart = time(); 
                            $tokenExpire = $tokenStart + 60*60; 
                            //save the token in a session variable (base 64 encoded)
                            $_SESSION['token'] = base64_encode($value); 

                            //For now write this new token, its start and expiry datetime into a .txt (appending not overwriting - this is for reference in case you loose your session data)
                            $file = "tokens.txt";
                            $fh = fopen($file, "a+");
                            //write the line in
                            $newLine = "'".$_SESSION['token']."','".date('d/m/Y H:i:s', $tokenStart)."','".date('d/m/Y H:i:s', $tokenExpire)."'"."\n";
                            fwrite($fh, $newLine);
                            //Close file
                            fclose($fh);
                        }

                }

                //If we have been given a token request XML from the API authenticating using the token
                if (!empty($_SESSION['token'])) {
                    connect($url);
                } else {
                    //If we have not been given a new token its because we already have a live token which has not expired yet (check the tokens.txt file)
                    echo '<br />There is still an active Token, you must wait for this token to expire before a new one can be requested!<br />';
                }
            }

            //Function to connect to the API authenticating ourself with the token we have been given
            function connect($url) {

                //If token is not set skip to else condition to request a new token 
                if(!empty($_SESSION['token'])) {

                    //Set a new file name and create a new file handle for our returned XML
                    $file = "test.xml";
                    $fh = fopen($file, "w");

                    //Initiate a new curl session
                    $ch = curl_init($url);
                    //Don't require header this time as curl_getinfo will tell us if we get HTTP 200 or 401
                    curl_setopt($ch, CURLOPT_HEADER, 0); 
                    //Provide Token in header
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$_SESSION['token']));
                    //Write returned XML to file
                    curl_setopt($ch, CURLOPT_FILE, $fh);
                    //Execute the curl session
                    curl_exec($ch);

                    //Store the curl session info/returned headers into the $info array
                    $info = curl_getinfo($ch);

                    //Check if we have been authorised or not
                    if($info['http_code'] == '401') {
                        getToken($url);
                        echo 'Token Failed - getToken() has been run!<br />';
                    } elseif ($info['http_code'] == '200') {
                        echo 'Token Worked - Success';
                    }

                    //Close the curl session
                    curl_close($ch);
                    //Close the open file handle
                    fclose($fh);

                } else {

                    //Run the getToken function above if we are not authenticated
                    getToken($url);

                }

            }

            //Connect to the API using connect() function above
            connect($request);

解决此问题的方法是输入xml的完整位置

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM