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

HTTP request failed due to stream context issue #13

Open
alphafer opened this issue Feb 10, 2015 · 6 comments
Open

HTTP request failed due to stream context issue #13

alphafer opened this issue Feb 10, 2015 · 6 comments

Comments

@alphafer
Copy link

It seems that the file_get_request is not longer opening the stream to send the API request with the proper parameters.

Warning: file_get_contents(https://pi.pardot.com/api/login/version/3/?Resource id #1): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad request in /PardotConnector.class.php on line 389

@stephenreid
Copy link
Owner

Can you provide any more context?

It looks as though you might be providing an object as opposed to string for the action (second parameter) see

send($module = 'prospect',$action = 'query',$parameters = array()){

@alphafer
Copy link
Author

Context:

The authenticate() method in PardotConnector class is the first method called to get the userKey. This method sends an array with the the parameters for the authentication API call

$params = array('email'=>$this->email,'password'=>$this->password,'user_key'=>$this->userKey);
ret = $this->send('login','',$params);

The send() method then prepares the API call and uses the $parameters to build the $context stream

$url = $baseurl.$module.'/'.$version.$action.'?'.
        $context = stream_context_create(array(
            'http'  => array(
                'method'    => 'POST',//never want to send credentials over GET
                'header'    => 'Content-type: application/x-www-form-urlencoded',
                'content'   => http_build_query($parameters),
                'timeout'   => 30.0, //in seconds
                'user_agent'=> 'PardotPHPClient',
                //'proxy'       => '',
                //'ignore_errors'   => false,
            )
        ));
        $res = file_get_contents($url,false,$context);

Then the API request sent throws a HTTP request failed! HTTP/1.0 400 Bad request

No changes from the original code were made. In fact, I cloned a fresh copy and tried to run test.php and got the same error.

@alphafer
Copy link
Author

Hi again.

I updated the send() method so it does not add the stream to the end of the url

$url = $baseurl.$module.'/'.$version.$action.'?'.http_build_query($parameters);
        $context = stream_context_create(array(.......

This worked. Any feedback?

@stephenreid
Copy link
Owner

I think that the . at the end of

$url = $baseurl.$module.'/'.$version.$action.'?'.

looks like a typo and should be a ; like

$url = $baseurl.$module.'/'.$version.$action.'?';

There isn't much of a reason to include the parameters here as the context will include that on file get content.

See also (the magic-ified class) https://github.com/stephenreid/Pardot-Api-Connector-PHP/blob/master/PardotConnector.class.php#L196

@stephenreid
Copy link
Owner

Can you Pr and I'll merge?

@alphafer
Copy link
Author

Hi. So you want me to fork and you will merge?

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