twitter-php-ads-sdk icon indicating copy to clipboard operation
twitter-php-ads-sdk copied to clipboard

HTTP request failed! HTTP/1.0 426 Upgrade Required

Open eduardoblucas opened this issue 4 years ago • 5 comments
trafficstars

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

eduardoblucas avatar Jan 21 '21 17:01 eduardoblucas

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.
                        }
                    }

theorytank avatar Jan 27 '21 18:01 theorytank

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 [email protected] 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 avatar Jan 27 '21 19:01 hborras

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);
                }
            }`

hborras avatar Feb 07 '21 09:02 hborras

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

eduardoblucas avatar Mar 02 '21 16:03 eduardoblucas

Hi @eduardoblucas

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

hborras avatar Mar 02 '21 16:03 hborras