I'm using the PHP sample code provided by Paypal for Paypal Payments Pro or Direct Pay. In a recent email from Paypal, they state
Starting October 7, 2013, we will require all incoming requests to have a “Host” header which complies with HTTP 1.1 Specifications. This header was not required under HTTP 1.0. IPN and PDT scripts using HTTP 1.0 may start failing with “HTTP/1.0 400 Bad Request” errors after October 7, 2013, which will result in IPN messages not being validated successfully, or PDT scripts not being able to retrieve transaction information.
They give me the following code to use as the header:
$header="POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .="Content-Type: application/x-www-form-urlencoded\r\n";
$header .="Host: www.paypal.com\r\n";
$header .="Connection: close\r\n\r\n";
but using CURL, how can I pass all of that?
I know I can set most of that in an array and put it under the CURLOPT_HEADER but what about that FIRST line?
Here's my code. Please help me understand how to correctly add the new header request.
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_URL, $api_endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $nvp_string);
$result = curl_exec($curl);
curl_close($curl);
Currently the headers are being stored in a string. To send custom headers with cURL, you can uset curl_setopt with CURLOPT_HTTPHEADER as follows:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"POST /cgi-bin/webscr HTTP/1.1\r\n",
"Content-Type: application/x-www-form-urlencoded\r\n",
"Host: www.paypal.com\r\n",
"Connection: close\r\n\r\n"
)
);
Related
Setting up a JSON-RPC on my vps which I want to connect via PHP CURL on my website doing a basic request and looking for getmasternodecount.
Tried many scripts and libraries before however none seems to work in my case. Now I try to write some basic php code, but this skill isnt my best.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function coinFunction () {
$feed = 'http://user:pass#ip/';
$post_string = '{"method": "getmasternodecount", "params": []}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_PORT, port);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/stratum', 'Content-length: '.strlen($post_string)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-length: '.strlen($post_string)));
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$data = coinFunction();
var_dump($data);
echo $data;
?>
And gives me this data dump:
string(127) "HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1 " HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1
When i delete all the var dump information etc, it send me a whitepage and sometimes NULL.
Kindly Regards,
Let's work with the first snippet. Since it's a POST request, file_get_contents is rather out of place here. Add the following setopt lines:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
Without those, the result of curl_exec won't contain the returned content.
It would also be advisable to specify the Content-Type of the request (which is application/json). The server might handle it even without, but just in case:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'));
Authentication is another thing. Credentials in the URL suggest Basic, but the server might expect otherwise... See CURLOPT_HTTPAUTH.
The Response should be XML, but i'm getting error like (56): Failure when receiving data from the peer while sending the request to REST API using headers, Here is the sample request according to the client
POST http://api.toyotautrust.in/1.0/olx/inventory HTTP/1.1
User-Agent: Fiddler
Authorization: Token ******-****-****-****-***********
Host: api.toyotautrust.in
Content-Length: 52
Here is my Request code written in PHP using cURL
$headers1=[
'POST /1.0/olx/inventory HTTP/1.1',
'Host: api.toyotautrust.in',
'User-Agent: Fiddler',
'Authorization: Token' .$atoken1,
'Content-Length: 52'];
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, 'http://api.toyotautrust.in/1.0/olx/inventory');
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_HEADER, true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_HTTPHEADER,$headers1);
$response1 = curl_exec($ch1);
print_r($response1);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $status_code1;
I have failed to send these two header in POST request
'User-Agent:Fiddler',
"Content-length: 0",
After adding these two headers, it's working fine. Before sending your request to server, Please ask server side people about headers and parameters
This question is roughly equivalent to the issue addressed here:
http://curl.haxx.se/mail/lib-2010-08/0118.html
The point is that some web services (inconveniently) require POSTing
empty HTTP header values. So, for example, you may have a RESTful API
that requires this HTTP header for a POST:
Content-Type: text/json
Content-Length: 1024
...
Custome-Header-Field: hello
Required-Empty-Header-Field:
...
Connection: Keep-Alive
The point here is that Required-Empty-Header-Field must be specified, and must be empty.
How do you do this in curl_exec within a PHP context?
I am going to answer my own question, as this took me a while to figure out.
But I'm curious to see what other programmers have to say.
// Assume we are POSTing data from the variable $data.
$http_header = array(
"Content-Type: application/x-www-form-urlencoded" . "\r\n" .
"Content-Length: ". strlen($data) . "\r\n" .
...
"Custome-Header-Field: hello" . "\r\n" .
"Required-Empty-Header-Field:" . "\r\n" .
...
"Connection: Keep-Alive",
"Other-Value-One: world",
"Other-Value-Two: thisworks");
// Add optional headers.
if (!empty($some_condition)) {
array_push($http_header, "Other-Value-Three: whatever");
}
// Set up curl_exec.
$curl = curl_init();
$url = "https://www.somewhere-over-the-rainbow.com";
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $httpHeader);
$response = curl_exec($curl);
curl_close($curl);
I haven't tested this, but try just setting a blank space for the value. This may get past any validation cURL is doing, and whitespace prior to a value in the headers is supposed to be ignored.
I'm currently trying to implement oAuth on a server side in order to provide an API for developers. I'm experiencing a very easy issue. I want to be able to handle HTTP headers sent to a script called request.php.
I have no idea how I can do that. I'm a coding a wrapper for clients, and try to make http call on request.php with curl.
$data = array('name' => 'Foo');
$header = array('Content-type: text/plain', 'Content-length: 100');
$ch = curl_init("test");
curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/request.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$res = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
So, in $headers I received the http responses headers but what I want to do is handling headers received by request.php.
You should use
curl_setopt($s, CURLOPT_HEADER, true);
this will cause $res in you code to have both the headers and the data seperated by 2 CRLF (4 chars in total as defined in HTTP standards).
HTTP Response example,
HTTP/1.0 302 Found
Content-Type: text/html; charset=UTF-8
Content-Length: 11782
Date: Tue, 13 Dec 2011 15:07:19 GMT
Server: GFE/2.0
<!DOCTYPE html>
<html>
(...)
</html>
Use curl_getinfo to read headers.
It is not necessary to set (CURLOPT_HEADER, true) to do this.
For example:
...
curl_setopt($this->ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($this->ch);
$httpCode = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
I am using PHP CURL to post some xml data to another server using that IP, i fullfilled all the requirements for making it work, but still getting an error of "BAD REQUEST".
Have a look on the code below.
$var2 ="<doc><item>Some content.</item></doc>";
$url = "server IP";
$header = "POST HTTP/1.1 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($var2)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $var2;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $var2);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
// Get Response
$data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
echo $data;
First, you are using this as request-line :
$header = "POST HTTP/1.1 \r\n";
According to the HTTP 1.1 RFC, the **Request-Line** should look like this :
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
So, it seems you should add an URI between POST and HTTP/1.1
(But, before coding that, read what I've written in the next paragraphs of my answer)
Then, you are passing a lot of things as CURLOPT_CUSTOMREQUEST.
I don't think you can pass all those header using that option (quoting) :
Valid values are things like "GET", "POST", "CONNECT" and so on;
i.e. Do not enter a whole HTTP request line here. For
instance, entering "GET /index.html HTTP/1.0\r\n\r\n" would be
incorrect.
If you want to specify custom headers (it seems you do), you should use the CURLOPT_HTTPHEADER option, instead of CURLOPT_CUSTOMREQUEST (quoting the documentation of the first one) :
An array of HTTP header fields to set, in the format
array('Content-type: text/plain', 'Content-length: 100')