Php http request parameters

How do I post query parameters with PHP?

I am trying to interface with the Clickbank API for support ticket creation (https://api.clickbank.com/rest/1.2/tickets/). I can’t seem to get it to work with CURL. I can get the API working fine using GET (fetching order information), but not POST (required for changes to orders). Here’s my code, which returns an error 404 from Clickbank.

I’ve been able to POST successfully with RESTClient2.3. But I haven’t been able to duplicate that with CURL.

With HTTP POST the query string goes in the request body, not in the url. Take out the ?type=cncl&reason=ticket.type.cancel.7 from the url and instead put it into the POST body (without the ? prefix). I can’t recall off the top of my head the exact PHP curl syntax.

should you be taking your query string params (type=cncl&reason=ticket.type.cancel.7) out and make them the request’s post body?

Here is the error message I am getting on now that I added curl_setopt($ch, CURLOPT_POSTFIELDS, «type=cncl&reason=ticket.type.cancel.7»); «The clickbank api does not support parameters passed in within request body. Please use query parameters instead.»

3 Answers 3

My wild guess is that RestClient tries to be smart and actually transforms your get URL parameters to post variables on the fly (I could be wrong though).

Anyhow. POST, you’re doing it wrong.

A curl POST request should be something like this:

I tried this example but getting below error: HTTP/1.1 400 Bad Request Date: Thu, 14 Aug 2014 07:13:52 GMT Server: Apache/2.2.27 (FreeBSD) mod_jk/1.2.40 mod_ssl/2.2.27 OpenSSL/0.9.8y Vary: Accept-Encoding Connection: close Transfer-Encoding: chunked Content-Type: text/plain A post method was executed with a request body. (Possibly post parameters). The clickbank api does not support parameters passed in within request body. Please use query parameters instead. Request Body: »1
I am tired of this almost spent a week on this but no luck please help me.

Hi Ben, i posted this as seperate question here stackoverflow.com/questions/25304556/… please help me to get out of it. Thanks.

Читайте также:  Twig to php online

After about 10 and a half hours of trial and error. I figured it out.

The problem was two prong.

    I am running PHP 5.2.4 which has a bug accepting custom headers. I found this code, which worked perfectly.

 if(version_compare(PHP_VERSION, '5.3.0') == -1) < ini_set('user_agent', 'PHP-SOAP/' . PHP_VERSION . "\r\n" . $options['header']); >

Here is the final code I ended up with, which works perfectly.

This interfaces with the Clickbank API via POST. I hope this helps a lot of people.

 array( 'method' => "POST", 'header' => "Accept: application/xml\r\n" . "Authorization: DEV-key-here:API-key-here\r\n" ) )); if(version_compare(PHP_VERSION, '5.3.0') == -1) < ini_set('user_agent', 'PHP-SOAP/' . PHP_VERSION . "\r\n" . $options['header']); >$context = stream_context_create($options); $xml = file_get_contents('https://api.clickbank.com/rest/1.2/tickets/VZR9RYE3?reason=ticket.type.cancel.7&type=cncl', false, $context); ?> 

I tried this but doesn’t work it gives me just blank screen. I posted a seperate question here stackoverflow.com/questions/25304556/… for this, Please help me.

 $mixCurlOptValue) < curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue); >$mixResponse = curl_exec($mixCH); curl_close($mixCH); return $mixResponse; > // If you need any HTTP authentication $username = 'http-auth-username'; $password = 'http-auth-password'; $requestType = 'POST'; // This can be PUT or POST // This can be $arrPostData = $_POST; $arrPostData = array( 'key1' => 'value-1-for-k1y-1', 'key2' => 'value-2-for-key-2', 'key3' => array( 'key31' => 'value-for-key-3-1', 'key32' => array( 'key321' => 'value-for-key321' ) ), 'key4' => array( 'key' => 'value' ) ); // You can set your POST data $postData = http_build_query($arrPostData); // Raw PHP array $postData = json_encode($arrPostData); // Only USE this when requesting JSON data $arrResponse = executeCurl(array( CURLOPT_URL => 'http://whatever-your-request-url.com/xyz/yii', CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPGET => true, CURLOPT_VERBOSE => true, CURLOPT_AUTOREFERER => true, CURLOPT_CUSTOMREQUEST => $requestType, CURLOPT_POSTFIELDS => $postData, CURLOPT_HTTPHEADER => array( "X-HTTP-Method-Override: " . $requestType, 'Content-Type: application/json', // Only USE this when requesting JSON data ), // If required HTTP authentication, use the below lines CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $username . ':' . $password )); 

Источник

Читайте также:  Что определяет html файл
Оцените статью