PHP函数没有正确返回变量

时间:2016-12-13 18:55:10

标签: php function variables curl

我对此代码有困难:

function convert($file, $name){
    global $apiKey, $uploadDir, $fname, $lname, $fileNam, $wpdb, $ewd_feup_user_fields_table_name, $conn, $usrID;

    $endpoint = "https://api.zamzar.com/v1/jobs";
    $sourceFilePath = $file;
    $targetFormat = "html5";

    $sql = "SELECT Field_Value FROM $ewd_feup_user_fields_table_name WHERE Job_ID ='$jobId' AND User_ID = '$usrID' ";
    $result = $conn->query($sql);

    //CHECK TO SEE IF THEY HAVE ALREADY CONVERTED FILE
    if ($result->num_rows > 0){
        // echo "The file you want to convert Already Exists";
        return "not here";
    }
    //ELSE CONVERT AND WRITE JOB ID TO SERVER
    else{
        // Since PHP 5.5+ CURLFile is the preferred method for uploading files
        if(function_exists('curl_file_create')) {
          $sourceFile = curl_file_create($sourceFilePath);
        } else {
          $sourceFile = '@' . realpath($sourceFilePath);
        }

        $postData = array(
          "source_file" => $sourceFile,
          "target_format" => $targetFormat
        );

        //PULL FILE INFO AS ARRAY
        $ch = curl_init(); // Init curl
        curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // Enable the @ prefix for uploading files
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
        curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
        $body = curl_exec($ch);
        curl_close($ch);
        $response = json_decode($body, true);

        //get ID from array
        $jobID = $response['id'];

        //update on server
        $sql = "UPDATE $ewd_feup_user_fields_table_name SET Field_Value='$jobID' WHERE User_ID='$usrID' AND Field_Name='Job ID' ";
        $conn->query($sql);

        //return
        return $jobID;      
    }   
}`

此代码只是从源中提取数组并检索job的ID。检索后,我将其返回$jobID。 以下代码应该使用$jobID来收集另一个名为$fileID的ID:

function get_file_id($z){
    global $apiKey, $uploadDir, $fname, $lname, $fileNam, $wpdb, $ewd_feup_user_fields_table_name, $conn, $usrID;

    $endpoint = "https://api.zamzar.com/v1/jobs/$z";

    $ch = curl_init(); // Init curl
    curl_setopt($ch, CURLOPT_URL, $endpoint); // API endpoint
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
    curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":"); // Set the API key as the basic auth username
    $body = curl_exec($ch);
    curl_close($ch);

    $job = json_decode($body, true);

    $fileI = $job['target_files']['0']['id'];

    $sql = "UPDATE $ewd_feup_user_fields_table_name SET Field_Value='$fileI' WHERE User_ID='$usrID' AND Field_Name='File ID' ";
    $conn->query($sql);

    echo $fileI;
    //echo "Done.";
}

当我尝试使用convert();中返回的值时,get_file_id();代码不会返回任何内容。

$conv = convert($uploadDir."Carlos Hernandez.docx", "Carlos Hernandez.docx");
get_file_id($conv);

0 个答案:

没有答案