HI I'm tring to send some headers in my PHP script such as
$headers[] = "BATCH_TYPE: XML_SINGLE";
$headers[] = "VENDOR_ID: 56309";
But they are being received as:
Batch-Type
Vendor-ID
..not as they were intended or required - which is causing me problems.
Anyone know why or how to sort?
Thanks,
<?php
function httpsPost($Url, $xml_data, $headers)
{
// Initialisation
$ch=curl_init();
// Set parameters
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $Url);
// Return a variable instead of posting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD,"username:password");
// Activate the POST method
curl_setopt($ch, CURLOPT_POST, 1) ;
// Request
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_TIMEOUT, 999);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// execute the connexion
$result = curl_exec($ch);
// Close it
curl_close($ch);
return $result;
}
$request_file = "./post_this.xml";
$fh = fopen($request_file, 'r');
$xml_data = fread($fh, filesize($request_file));
fclose($fh);
$url = 'http://www.xhaus.com/headers';
$headers = array();
$headers[] = "Expect:";
$headers[] = "Accept: text/xml";
$headers[] = "BATCH_TYPE: XML_SINGLE";
$headers[] = "BATCH_COUNT: 1";
$headers[] = "VENDOR_ID: 54367";
$Response = httpsPost($url, $xml_data, $headers);
echo $Response;
?>
How are you checking these headers ? I've just tried myself with the following code:-
<?php
header("BATCH_TYPE: XML_SINGLE");
And got back the following:-
HTTP/1.1 200 OK
Date: Wed, 10 Jun 2009 08:56:54 GMT
Server: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.1 with Suhosin-Patch
X-Powered-By: PHP/5.2.6-3ubuntu4.1
BATCH_TYPE: XML_SINGLE
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html
Use fsockopen() and write/read manually anything you need. I'm not sure if your implementation of CURL or smth. else like proxy doesn't change your headers. If you want to do something good for sure - just do it yourself ;). Actually it's so easy to create HTTP-request and write it to opened socket...
$req = "POST $url HTTP/1.0\n";
$headers[] = 'VENDOR_ID: 1234';
$headers[] = 'MY_OTHER_HEADER: xxxxx';
$req .= implode("\n", $headers);
$req .= "\n\n" . $request_body;
$sock = fsockopen($host, 80, $errno, $errstr, $timeout);
fwrite($sock, $req, strlen($req));
$return = '';
do
{
$return .= fread($sock, 512);
} while (!feof($sock));
fclose($sock);
Not sure, but it seems to me, that smth. like this has already been done somewhere in PEAR...
Change the following code:
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
To this:
foreach($headers as $header)
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
After a week of battling with the company of this external server, they were in fact giving me the wrong headers - D'oh!
Related
I write a php script to upload a media file but when I use it the response occurs:
Missing Content-Length header.
The php code is:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://raz-ul.domenka.com/v1/AUTH_8a619275-f933-4da1-b289-0ca1a2a3a4a5/bcd/drop1.avi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "X-Auth-Token: AUTH_tkd78bedda5bec4613b80becb1b2b3b4b5";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$header_size = $info['header_size'];
$header = substr($response, 0, $header_size);
var_dump($header);
On the other hand When I do something like that in cli I succeed:
kinduser#43915-1-c1c2c3-01:~/public_html/adam$ curl -i -X PUT -H "X-Auth-Token: AUTH_tkd78bedda5bec4613b80bec0123456789" -T drop.avi https://raz-ul.domenka.com/v1/AUTH_8a619275-f933-4da1-b289-0ca1a2a3a4a5/bcd/drop.avi
HTTP/1.1 100 Continue
HTTP/1.1 201 Created
Last-Modified: Thu, 16 Feb 2017 10:12:54 GMT
Content-Length: 0
Etag: fa60ba1b78299bfae5ac61d1d2d3d4d5
Content-Type: text/html; charset=UTF-8
X-Trans-Id: tx114b22027223415c850db-e1e2e3e4e5
Date: Thu, 16 Feb 2017 10:12:53 GMT
kinduser#43915-1-c1c2c3-01:~/public_html/adam$
But I would like to do this in php. How to fix my php code?
Update
I added some code to tell curl which file to send. It sent the file but the file changed on the server. It got a bigger weight from 660kB to 660.31kB. After downloading to my pc it got impossible to open via video player.
That code is like this now:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://raz-ul.domenka.com/v1/AUTH_8a619275-f933-4da1-b289-0ca1a2a3a4a5/bcd/drop7.avi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "X-Auth-Token: AUTH_tkd78bedda5bec4613b80becb1b2b3b4b5";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curlFile = curl_file_create ('./drop.avi');
$post = array (
'extra_info' => '123456',
'file_contents' => $curlFile
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$header_size = $info['header_size'];
$header = substr($response, 0, $header_size);
var_dump($header);
Any idea how to fix it?
I want to fetch some data from 12.96.12.174 this IP address using http post by XML. I create curl PHP code for that
<?php
$url = "http://12.96.12.174";
$post_string = '<?xml version="1.0"?>
<CRMAcresMessage>
<Header ImmediateResponseRequired="true" OriginalBodyRequested="true">
<MessageID>25</MessageID>
<TimeStamp>2016-03-11T011:35:11</TimeStamp>
<Operation Operand="Request" Data="PlayerProfile" />
</Header>
<PlayerID>59979</PlayerID>
<Body>
</Body>
</CRMAcresMessage>';
$header = "POST HTTP/1.0 \r\n";
$header .= "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n";
$header .= $post_string;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,$url);
//$fp = fopen('rss.xml', 'w');
curl_setopt($ch,CURLOPT_PORT, 8085);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
echo $data = curl_exec($ch);
if(curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);
?>
When I hit that link from "http://example.com/members/check.php" this url this gives me Recv failure: Connection was reset in Curl.
And from my localhost I got Failed to connect
php version is also >5.3
I am beginner in Curl.
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
For Recv failure: Connection was reset in Curl PHP ==>
Declare like this :
CURLOPT_SSL_VERIFYPEER as 0
& make sure that you are using PHP version >5.3
I am trying to send a PATCH request through cURL and i get this response:
HTTP/1.1 400 Bad Request Server: nginx Date: Fri, 01 Apr 2016 09:16:03 GMT Content-Type: application/problem+json Content-Length: 198 Connection: keep-alive X-Powered-By: PHP/5.6.19 {"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title":"Internal Server Error","status":500,"detail":"setValidationGroup() expects a list of valid input names; \"\" was not found"}
My code:
sendData("2016-10-01", "2016-10-02", "1234");
function sendData($from, $to, $property){
$data = array(
"from" => $from,
"to" => $to,
"booking_id" => 3
);
$data = json_encode($data);
echo $data . "<br>";
$url = "https://api.urlhost.com/property/".$property."/avail";
echo $url. "<br>";
$headers = array('Authorization: Basic dGVhQGJsdW1lbnJpdmllcmEuOIT6Qmx1bQVucm17aAKyzTtyfw==','Content-Type: application/json-patch+json');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);
echo ($response) . PHP_EOL;
//print_r(curl_getinfo($ch));
curl_close($curl);
}
When i echo the data and url i get these:
{"from":"2016-10-01","to":"2016-10-02","booking_id":3}
https://api.urlhost.com/property/1234/avail
But still is giving me this error. Any suggestions why this might happen?
I am trying to call a web service of a shipping company in my php code and get the result xml. I have this sample code and i want to know if there is an alternative using curl.
Code:
function doPost($_postContent) {
$postContent = "xml_in=".$_postContent;
$host="test.company.com";
$contentLen = strlen($postContent);
$httpHeader ="POST /shippergate2.asp HTTP/1.1\r\n"
."Host: $host\r\n"
."User-Agent: PHP Script\r\n"
."Content-Type: application/x-www-form-urlencoded\r\n"
."Content-Length: $contentLen\r\n"
."Connection: close\r\n"
."\r\n";
$httpHeader.=$postContent;
$fp = fsockopen($host, 81);
fputs($fp, $httpHeader);
$result = "";
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
// close the socket connection:
fclose($fp);
$result = explode("\r\n\r\n", $result,3);
}
Can i call it using curl?
You can use the CURLOPT_PORT option to change the port to 81. See http://php.net/manual/en/function.curl-setopt.php
$url = "http://test.company.com/shippergate2.asp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 81);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "PHP Script");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postContent);
$data = curl_exec($ch);
I guess a complete solution is needed but I suggest checking this basic CURL wrapper for PHP https://github.com/shuber/curl
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);