hborras / twitter-php-ads-sdk

A Twitter supported and maintained Ads API SDK for PHP.
55 stars 41 forks source link

HTTP request failed! HTTP/1.0 426 Upgrade Required #86

Open eduardoblucas opened 3 years ago

eduardoblucas commented 3 years ago

Hi, I'm getting this error now. Any ideas? thanks

ErrorException: gzfile(https://ton.twimg.com/advertiser-api-async-analytics/slKanmA6oZyP8UlbYrjfkN6tbke2ZMQVFp_d1sWiQWKfpLiGd4AuannFvvEFS0Kv7s55-2n4sl7q7aawZy2kW7o6ZQ-jMp_PrA_7I9Lyx_UiuuiFSAlbfZwJbufS7RAg.json.gz): failed to open stream: HTTP request failed! HTTP/1.0 426 Upgrade Required

theorytank commented 3 years ago

I was having the same issue when requesting the payload from a async job. The response from $job->getUrl() returns a URL that you are probably passing directly to the gzfile() function. I've noticed that there's a timing issue (on the Twitter side) that the GZ'd payload at the getUrl() location isn't ready. So, on failure, if you sleep for a second and try again (a few times) eventually the payload returns.

Rough example of a solution:

                    $job->read();

                    if ($job->getStatus() == JobFields::SUCCESS) {

                         $cnt = 5; // 5 attempts before abandoning
                        $result = false;

                        while($cnt--) {
                            $result = @gzfile($job->getUrl());
                            if( !$result ) {
                                sleep(1);
                            } else {
                                break; // Leave while loop because we have a result
                            }
                        }

                        if( $result ) {
                            // Do your processing here.
                        }
                    }
hborras commented 3 years ago

Thanks for your message, I haven't got enough time to take a deeper look at it.

I'll check your solution

Thanks again!

Missatge de Josh notifications@github.com del dia dc., 27 de gen. 2021 a les 19:57:

I was having the same issue when requesting the payload from a async job. The response from $job->getUrl() returns a URL that you are probably passing directly to the gzfile() function. I've noticed that there's a timing issue (on the Twitter side) that the GZ'd payload at the getUrl() location isn't ready. So, on failure, if you sleep for a second and try again (a few times) eventually the payload returns.

Rough example of a solution:

                $job->read();

                if ($job->getStatus() == JobFields::SUCCESS) {

                     $cnt = 5; // 5 attempts before abandoning
                    $result = false;

                    while($cnt--) {
                        $result = @gzfile($job->getUrl());
                        if( !$result ) {
                            sleep(1);
                        } else {
                            break; // Leave while loop because we have a result
                        }
                    }

                    if( $result ) {
                        // Do your processing here.
                    }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/hborras/twitter-php-ads-sdk/issues/86#issuecomment-768500573, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATKEBB6SCQIU7QFEDDWR4TS4BOZRANCNFSM4WNIQHZA .

hborras commented 3 years ago

Checked it and it's working:

This is the test code I'm using:

`$job = $lineItem->stats( [ TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_BILLING, TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_MOBILE_CONVERSION, TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_ENGAGEMENT, ], [ TwitterAds\Fields\AnalyticsFields::START_TIME => $date[0], AnalyticsFields::END_TIME => $date[1], AnalyticsFields::GRANULARITY => Enumerations::GRANULARITY_TOTAL, AnalyticsFields::PLACEMENT => Enumerations::PLACEMENT_ALL_ON_TWITTER ], $async ); while($job->getStatus() == TwitterAds\Fields\JobFields::PROCESSING){ echo 'Job is still processing. Waiting 5 more seconds' .PHP_EOL; $job->read(); sleep(5); }

            if($job->getStatus() == TwitterAds\Fields\JobFields::SUCCESS){
                $result = gzfile($job->getUrl());
                $result = implode('', $result);
                $stats = json_decode($result)->data;
                $stats = $stats[0]->id_data[0]->metrics;
                if (!is_null($stats->billed_charge_local_micro)) {
                    echo "\t\t\t Start: " . $date[0]->format('Y-m-d H:i:s') . PHP_EOL;
                    echo "\t\t\t End: " . $date[1]->format('Y-m-d H:i:s') . PHP_EOL;
                    echo "\t\t\t " . ($stats->billed_charge_local_micro[0] / 1000000) . '€' . PHP_EOL;
                    echo "\t\t\t\t App clicks: ";
                    getStats($stats->app_clicks);
                    echo "\t\t\t\t Installs:" . PHP_EOL;
                    getStats($stats->mobile_conversion_installs);
                    echo "\t\t\t\t Checkouts:" . PHP_EOL;
                    getStats($stats->mobile_conversion_checkouts_initiated);
                }
            }`
eduardoblucas commented 3 years ago

Hi any idea why this happens sometimes?

It is happening again and I didn't change anything.

ErrorException: gzfile(https://ton.twimg.com/advertiser-api-async-analytics/tOMs85xJqNl4ZqFzCkGYyYFmgwirELTozxz3Vkw7I8cPeVZnyDgcusfLPs9E9IPJmM_v6SqK07vsXuu-8Ko5_gfG8XmkI_sphweYDLBrjzr43raU9D1GhtXjS_21p_0L.json.gz): failed to open stream: HTTP request failed! HTTP/1.0 426 Upgrade Required

hborras commented 3 years ago

Hi @eduardoblucas

If it happens "somentimes" it could be an error on Twitter's side, what do you think?