Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use the timeout method? #97

Closed
eiyu opened this issue Mar 20, 2016 · 3 comments
Closed

how to use the timeout method? #97

eiyu opened this issue Mar 20, 2016 · 3 comments

Comments

@eiyu
Copy link

eiyu commented Mar 20, 2016

How do we use Unirest timeout method, I have this code but its not working

 public function __construct($api_key, $additional_headers = array()) {
        Unirest::timeout(200);
        AbcClass::$api_key = $api_key;
        Unirest::defaultHeader("Content-Type", "application/x-www-form-urlencoded");
        Unirest::defaultHeader("key", AbcClass::$api_key);
        foreach ($additional_headers as $key => $value) {
        Unirest::defaultHeader($key, $value);

        }
    }

I also try

    function getCost($origin, $destination = array(),$weight, $courier = NULL) {
    Unirest::timeout(200);
    $params = array(
        'origin' => $origin,
        'destination' => $destination,
        'weight' => $weight
    );
    if (!is_null($courier)) {
        $params['courier'] = $courier;
    }

    return Unirest::post(AbcClass::$base_url . "cost", array(), http_build_query($params));
}

}

@ahmadnassri
Copy link
Contributor

I suggest you review the README docs again, your code for making a POST request is wrong.

@eiyu
Copy link
Author

eiyu commented Mar 21, 2016

Hi, Thank you again @ahmadnassri, i've try to put the timeout method in the constructor and it doesn't work and then actually I have no idea where to put the timeout method again,
maybe this code is not good practice but check this out (i'm using laravel)

        // get all shipping cost from one city to every city
        Route::get('/{origin}/{courier}', function ($origin,$courier) {

        $client = new AbcClass("apikey");

        $cities = $client->getCity();
        $body = $cities->body;
        //get allcity array
        $allCity = $body->abcapi->results;
        //make new post request for each city
        foreach ($allCity as $city) {
    $costs = $client->getCost($origin, $city->city_id, 1000, $courier);
        $value = $costs->body;
    $cost = $value->abcapi->results[0]->costs[0]->cost[0]->value;

    echo $city->city_id . "," . $city->city_name . "," . $cost . "<br/>";
     }

the code is working but it stops in the middle because of maximum exceed 60 sec.
Do you have suggestion?
Sorry for asking this in here.

@ahmadnassri
Copy link
Contributor

sounds like the time out on the PHP level is still triggering, not unirest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants