Blessan Kurien
Blessan Kurien

Reputation: 1665

Php: Api returns bad request error message

I am trying to get list of videos using youtube apiv3, but i will get a bad request if publishedAfter parameter present in the request.

According to their documenation

Type: datetime

The publishedAfter parameter indicates that the API response should only contain resources created after the specified time. The value is an RFC 3339 formatted date-time value (1970-01-01T00:00:00Z).

So i convert the date parameter to RFC3339 format Here is my code

    $time="2016-06-21 05:40:00";
    $datetime = \DateTime::createFromFormat("Y-m-d H:i:s", $time);
    //RFC3339 format
    echo $t=(datetime)$datetime->format(\DateTime::RFC3339);

Here is the url for getting result

 $url='https://www.googleapis.com/youtube/v3/search?part=snippet&order=viewCount&key={kEY}&&publishedAfter='.$t;

I will get this error

Array
(
    [error] => Array
        (
            [errors] => Array
                (
                    [0] => Array
                        (
                            [domain] => global
                            [reason] => badRequest
                            [message] => Bad Request
                        )

                )

            [code] => 400
            [message] => Bad Request
        )

)

Note:

Without publishedAfter parameter i will get response, so i think the problem with dateformat.

Upvotes: 0

Views: 465

Answers (1)

Manikanta Siripella
Manikanta Siripella

Reputation: 672

Please try something below.

$time="2016-06-21 05:40:00";
    $datetime = new DateTime($time);
    $datetime->format('Y-m-d\TH:i:sP');

Upvotes: 1

Related Questions