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

Add setUrl() method #82

Closed
hackel opened this issue Sep 15, 2014 · 5 comments
Closed

Add setUrl() method #82

hackel opened this issue Sep 15, 2014 · 5 comments

Comments

@hackel
Copy link

hackel commented Sep 15, 2014

Typically when working with APIs, all requests are sent to the same URL. It doesn't make sense to specify this over and over again. There should be a single call to set the URL to use for every request.

@zachborboa
Copy link
Member

Thank you for the issue. Can you provide an example use case?

@hackel
Copy link
Author

hackel commented Sep 16, 2014

Anything in which you are performing multiple requests to the same URL. E.g.:

$curl->setUrl('http://somegreat/api');
$curl->post(array('name'=>'foo');
$curl->get(array('q'=>'keyword'));
$curl->patch(array('id'=>100, 'name'=>'bar');

It's not a requirement, I can certainly include the URL in every request (which I am doing now), it just seems like it might be a natural/elegant method to add. Even what I wrote above seems too procedural. Possibly chaining them like this might be better:

Curl::url('http://somegreat/api')
    ->setOpt(CURLOPT_AUTOREFERER, true)
    ->setCookie(...)
    ->get(...);

@zborboa-g
Copy link
Contributor

Here is how this could be implemented

$curl = new Curl();
$curl->setUrl('https://api.example.com/inbox/');

// OR

$curl = new Curl('https://api.example.com/inbox/');

And the usage:

for ($i = 1; $i <= 10; $i++) {
    $curl->get(array(
        'page' => $i,
    ));
}

However, there would be a bit of ambiguity. For example, $curl->get() currently expects a url string or an array of urls as the first parameter. Using $curl->setUrl() and then calling $curl->get() with an array of data would be ambiguous as we would not know if the array passed is a set of urls or data.

@hackel
Copy link
Author

hackel commented Sep 17, 2014

Yes, I don't see an obvious way to maintain backward compatibility... You could possibly move the parallel processing into it's own get_parallel function or something, and then check if $url is an array in every function, and if so use it as $data. This might be too big a change, though.

@zborboa-g
Copy link
Contributor

Actually I believe there is a way this could work by using Curl::is_array_assoc() on the first parameter. A user is going to do parallel requests using get() with an indexed array and not with an associated array.

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

No branches or pull requests

3 participants