BingAds / BingAds-PHP-SDK

Other
56 stars 46 forks source link

Bulk Upload problem - always RequestStatus: Failed #57

Closed madsem closed 6 years ago

madsem commented 6 years ago

I'm trying all day to bulk upload keyword bid updates, but it won't work.

The file was not downloaded by the Bulk service before, I'm trying to work with data that is cached in a database and then build a csv from that to upload via Bulk service.

$file = fopen($this->csvPath, "w") or die('could not open file');
                fwrite($file, "\xEF\xBB\xBF");
                fwrite($file, "Parent Id,Client Id,Id,Bid,Name\r\n");
                fwrite($file, "Format Version,,,,5\r\n");
                foreach ($adgroups as $adGroupId => $keywords) {

                    foreach ($keywords as $keyword) {
                        $line = [
                            $adGroupId,
                            $GLOBALS['AuthorizationData']->CustomerId,
                            $keyword[0],
                            $keyword[1]
                        ];
                        fputcsv($file, $line);
                    }

                }
                fclose($file);

                $this->CompressFile($this->csvPath, $this->zipPath);

                // Use the bulk service to upload changes for this account
                $responseMode = ResponseMode::ErrorsAndResults;

                $uploadResponse = $this->GetBulkUploadUrl(
                    $responseMode,
                    $GLOBALS['AuthorizationData']->AccountId
                );

                $uploadUrl = $uploadResponse->UploadUrl;
                $uploadRequestId = $uploadResponse->RequestId;

                printf("Upload Request Id: %s\n", $uploadRequestId);
                printf("Upload Url: %s\n", $uploadUrl);

                $uploadSuccess = $this->UploadFile(
                    $uploadUrl,
                    $this->zipPath
                );

                // remove tmp file
                unlink($this->zipPath);
                unlink($this->csvPath);

                // If the file was not uploaded, do not continue to poll for results.
                if($uploadSuccess == false){
                    return;
                }

                // poll to get response
                for ($i = 0; $i < 100; $i++) {
                    sleep(5);

                    var_dump($this->GetUploadResultErrors($uploadRequestId));

                    // GetUploadRequestStatus helper method calls the corresponding Bing Ads service operation
                    // to get the upload status.
                    $uploadRequestStatus = $this->GetUploadRequestStatus($uploadRequestId);
                    print $uploadRequestStatus . "\r\n";

                    if (($uploadRequestStatus != null) && (($uploadRequestStatus == "Completed")
                            || ($uploadRequestStatus == "CompletedWithErrors"))) {
                        {
                            $uploadSuccess = true;
                            break;
                        }
                    }
                }

                if ($uploadSuccess) {

                    try {
                        // GetUploadResultFileUrl helper method calls the corresponding Bing Ads service operation
                        // to get the upload result file Url.
                        $uploadResultFileUrl = $this->GetUploadResultFileUrl($uploadRequestId);
                        $this->DownloadFile($uploadResultFileUrl, $this->tmpPath . '/results/');
                        printf("The upload result file was written to %s.\n", $this->tmpPath . '/results/');
                    } catch (\Exception $e) {
                        print "could not download file, error received: " . $e;
                    }

This is what I get as response

Upload Request Id: 5981cb70-921f-4f9e-ad4e-f049d2feef42
Upload Url: https://campaign.api.bingads.microsoft.com/Api/Advertiser/CampaignManagement/FileUpload/File/UploadBulkFile/5981cb70-921f-4f9e-ad4e-f049d2feef42
Upload Result:
{"TrackingId":"06144799-6fad-479e-bc17-388d75f2d74d","RequestId":"5981cb70-921f-4f9e-ad4e-f049d2feef42"}
HTTP Result Code:
200
object(stdClass)#28 (1) {
  ["OperationError"]=>
  array(1) {
    [0]=>
    object(stdClass)#29 (4) {
      ["Code"]=>
      int(3219)
      ["Details"]=>
      NULL
      ["ErrorCode"]=>
      string(32) "BulkServiceFormatVersionRequired"
      ["Message"]=>
      string(31) "The format version is required."
    }
  }
}
Failed

This is how my csv is formatted: (amount of keywords I'm trying to upload is between ~1,500 - 20,000 per account / campaign)

Parent Id,Client Id,Id,Bid,Name
Format Version,,,,5
1260040451463664,XXXXXXXX,78752594635885,0.95
1260040451465184,XXXXXXXX,78752594635985,0.19

Please help, urgently need to get this done so we can change bids but i'm at my wits end :(

madsem commented 6 years ago

nvm

J7mbo commented 6 years ago

For future viewers, Name and Type columns require Format Version and 5 values respectively. Which then also requires all the other fields that don't need to use the name and type columns to be blank. Great design!

madsem commented 6 years ago

thanks @J7mbo also have to mention, the order of column names has to be the exact same as in Microsofts examples and/or downloaded bulk files or it fails and just throws an error about Format version being required. Love it, very clear :)