cURL in PHP only partially removing headers - php

Running Out Of Sanity
I have looked high and low and lost a bit of my life and a lot of my sanity trying to return a cURL POST request. Everything works fine except I cannot remove the headers. Oh yes I have tried
curl_setopt($ch, CURLOPT_HEADER, false);
What worse is that when I try
$headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
I get a return of 25 which is completely wrong. Please help I am at my wits end with this one.
So I call this method
$service = new RestfulService($Url1);
$response = $service->request('', 'POST', $tokenXml, null,
array(CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING =>'UTF-8'));
Which in turn calls (using SilverStripe framework)
public function curlRequest($url, $method, $data = null, $headers = null, $curlOptions = array()) {
$ch = curl_init();
$timeout = 5;
$sapphireInfo = new SapphireInfo();
$useragent = 'SilverStripe/' . $sapphireInfo->Version();
$curlOptions = $curlOptions + (array)$this->config()->default_curl_options;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if(!ini_get('open_basedir')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
//include headers in the response
curl_setopt($ch, CURLOPT_HEADER, true);
// Add headers
if($this->customHeaders) {
$headers = array_merge((array)$this->customHeaders, (array)$headers);
}
if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Add authentication
if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, $this->getBasicAuthString());
// Add fields to POST and PUT requests
if($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} elseif($method == 'PUT') {
$put = fopen("php://temp", 'r+');
fwrite($put, $data);
fseek($put, 0);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_INFILE, $put);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
}
// Apply proxy settings
if(is_array($this->proxy)) {
curl_setopt_array($ch, $this->proxy);
}
// Set any custom options passed to the request() function
curl_setopt_array($ch, $curlOptions);
// Run request
$rawResponse = curl_exec($ch);
if (curl_error($ch)) var_dump(curl_error($ch));
$response = $this->extractResponse($ch, $rawResponse);
curl_close($ch);
return $response;
}
When I dump the response, the remove header command only removes the first few characters. Like so...
object(RestfulService_Response)[1065]
protected 'simpleXML' => null
protected 'cachedResponse' => boolean false
protected 'statusCode' => int 100
protected 'statusDescription' => string 'Continue' (length=8)
protected 'headers' =>
array (size=0)
empty
protected 'body' => string 'ontrol: no-cache
Pragma: no-cache
Content-Length: 3593
Content-Type: application/soap+xml; charset=utf-8
Expires: Fri, 20 Sep 2013 01:14:14 GMT
Server: Microsoft-IIS/7.5
P3P: CP="DSP CUR OTPi IND OTRi ONL FIN"
X-XSS-Protection: 0
PPServer: PPV: 30 H: CO1IDOLGN62 V: 0
Date: Fri, 20 Sep 2013 01:15:13 GMT
Connection: close
�<?xml version="1.0" encoding="utf-8" ?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envel
Has it got anything to do with the strange character before the
Update: I have checked this code on my colleagues computer, he doesn't have any problems. I am running WAMP my colleague is on a MAC, could there be any set up issues? I have downloaded the most recent curl.dll and added it to my php library, still no cigar.
Any ideas???

In my case the encoding was incorrect I needed to add the following option
curl_setopt($ch, CURLOPT_SSLVERSION, 3)
Works fine now

Related

PayPal NVP API - TransactionSearch Error 10001 Timeout processing request

I'm trying to build a custom accounting report using the PayPal NVP API that will get all transactions for a specific date range.
My code:
$headers = array(
'USER' => $production_user,
'PWD' => $production_pass,
'SIGNATURE' => $production_sig
);
$nvp = array(
'METHOD' => 'TransactionSearch',
'TRANSACTIONCLASS' => 'RECEIVED',
'STARTDATE' => '2016-12-01T00:00:00Z',
'ENDDATE' => '2016-12-31T00:00:00Z'
);
$request_url = "https://api-3t.paypal.com/nvp?".http_build_query($nvp);
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
$result = explode("&", $result);
foreach($result as $f=>$v){
$t = explode("=", $v);
echo $t[0]." => ".urldecode($t[1]);
echo "<br>";
}
Here is what gets printed:
HTTP/1.1 200 OK Date: Fri, 10 Feb 2017 19:51:20 GMT Server: Apache X-PAYPAL-OPERATION-NAME: X-PAYPAL-API-RC: 10001 Connection: close Cache-Control: max-age => 0, no-cache, no-store, must-revalidate Pragma: no-cache HTTP_X_PP_AZ_LOCATOR: slcb.slc Paypal-Debug-Id: 484a759b46e4a Set-Cookie: X-PP-SILOVER
CORRELATIONID => some_random_characters
ACK => Failure
L_ERRORCODE0 => 10001
L_SHORTMESSAGE0 => Internal Error
L_LONGMESSAGE0 => Timeout processing request
Any assistance with this issue would be greatly appreciated!
You are using a post request and have not post data.
I do not know what PayPal wants to see but I'm guessing it's not what you are sending.
Some things to try:
If you need to pass the USER, PWD, and SIGNATURE in the Request Header do it like this:
$request = array();
$request[] = "USER: $production_user";
$request[] = "PWD: $production_pass";
$request[] = "SIGNATURE: $production_sig";
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
If the $nvp parameters need to be post data, try this:
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp);
The $nvp can be passed as a query string in the post data also.
$query = http_build_query($nvp);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
The difference is the Content-Type.
The first method:
Content-Type: application/x-www-form-urlencoded
The second method:
Content-Type: multipart/form-data
To help in trouble shooting it would be good to see both the request and response header.
Use these options to get the headers:
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
The Request Header will be in the 'curl_getinfo()'
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
var_export($info);
The above gives lots of other details of the request. If you only want to see the header:
$request = curl_getinfo($ch, CURLINFO_HEADER_OUT);
To get the response header:
$result = curl_exec($ch);
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$response = substr($result ,0,$skip);
$result = substr($result ,$skip);

PUT upload php curl command response is "Missing Content-Length header."

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?

API REST post hash credentials and make GET, DELETE or PUT request

I'm working with this Phalcon PHP API HMAC framework and I have a little question.
I declared a GET route in routes.php and Im trying to make a request from client-connect.php (a simple CURL script to make requests).
That's how the client looks like:
$privateKey = '593fe6ed77014f9502761028801aa376f141916bd26b1b3f0271b5ec3135b989';
$time = time();
$id = 1;
$data = [
'name' => 'bob',
];
$message = buildMessage($time, $id, $data);
$hash = hash_hmac('sha256', $message, $privateKey);
$headers = ['API_ID: ' . $id, 'API_TIME: ' . $time, 'API_HASH: ' . $hash];
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL, $host);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
When I make a post request, all works ok, but when I try to make a GET, PUT or DELETE request (I just uncomment the line commented in client), it returns me an error:
Request:
DELETE /nodes/mikrotik HTTP/1.1
Host: vpn.wibee.com
Accept: */*
API_ID: 1
API_TIME: 1446031137
API_HASH: 4d0852239859da5e90270b3f7dfd2167f6e5153ca83a9c5896ee262e41b19674
Content-Length: 508
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------e71b5fd144802749
Response:
HTTP/1.1 100 Continue
HTTP/1.1 401 Unauthorized
Server: nginx/1.6.2
Date: Wed, 28 Oct 2015 11:18:58 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Access denied
I think it's posible im missing something (something related with authentication). Any solution?
Thanks!
I would try it. In original implementation of client app you have such a part:
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL, $host);
switch($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case 'GET':
break;
default:
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
break;
}
And your implementation is quite different. Once you are using POST method, functionality surprisingly work even with ($ch, CURLOPT_POST, FALSE). But Once using all other requests, you are running into troubles. It's because your implementation of GET does a few things it should not:
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
this is not correct for making GET requests with CURL. It surprisingly works, but you are receiving access denied. It is probably because authentication data, are not properly iterpreted on server side.
Also other methods will have some troubles to work, because you are mising this part of original code:
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
My best solution wold be to advise you, to try to use existing code sample instead of rewriting it by hand in buggy way. Extending that code for your use should not be complicated, especially if you are running it from shell.

How to capture a plain copy of my HTTP request

I am trying to communicate with anREST API using cURL and PHP. But, I am getting an issue from the API and I need to be able to see my HTTP request in plain text where I can analyses it and correct it.
I tried the following to write my request to a file
$file = fopen('request.txt', 'w');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $file);
But this does not show me the JSON data.
Here is what I get in the request.txt file
* Hostname servername was found in DNS cache
* Trying INTERNAL IP...
* Connected to servername (INTERNAL IP) port 8018 (#0)
> PUT /icws/1163386002/messaging/subscriptions/queues/blahblah HTTP/1.1
Host: servername:8018
Accept: */*
ININ-ICWS-CSRF-Token: WAhtYWxoYXlla1dBY2GHSDMNDFtjMDcwZmIyYy1mguc4LTQ3YjEtODgzMy1iOTBkM2ZhYWHfoyNmYTlYCjEwLjAuNC4xNjA=
ININ-ICWS-Session-ID: 1163386002
Cookie: icws_1163386002=232sggsdfgdabe-404c-8d8c-12345dfgdfg
Content-Length: 156
Content-Type: application/x-www-form-urlencoded
* upload completely sent off: 156 out of 156 bytes
< HTTP/1.1 500 Internal Server Error
< Cache-Control: no-cache, no-store, must-revalidate
< Pragma: no-cache
< Expires: 0
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Credentials: true
< Content-Type: application/vnd.inin.icws+JSON; charset=utf-8
< Date: Tue, 12 May 2015 17:52:52 GMT
< Server: HttpPluginHost
< Content-Length: 90
<
* Connection #0 to host servername left intact
I would like to see the JSON string in plain text.
Here is how I am doing my cURL call using PHP
private function _processRequest($method, $uri, $data = false, $header = NULL, &$httpRespond = array(), &$httpCode = NULL)
{
$ch = curl_init();
$url = $this->_baseURL . $uri;
if(
($method == 'POST' || $method == 'PUT')
&& $data
){
$jsonString = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $jsonString );
}
if($method == 'POST'){
curl_setopt($ch, CURLOPT_POST, true);
} elseif( $method == 'PUT'){
//curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
} else {
if ($data){
$url = sprintf("%s?%s", $url, http_build_query($data));
}
}
//disable the use of cached connection
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
//return the respond from the API
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//return the HEADER respond from the API
curl_setopt($ch, CURLOPT_HEADER, true);
//add custom headers
if(!empty($header)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
//enable SSL
if( $this->_protocol == 'https' ){
curl_setopt($ch, CURLOPT_CAINFO, $this->_cainfo);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
}
//set the URL
curl_setopt($ch, CURLOPT_URL, $url);
$file = fopen('request.txt', 'w');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $file);
$respond = curl_exec($ch);
//throw cURL exception
if($respond === false){
$errorNo = curl_errno($ch);
$errorMessage = curl_error($ch);
throw new ApiException($errorMessage, $errorNo);
}
list($header, $body) = explode("\r\n\r\n", $respond, 2);
$httpRespond = $this->_http_parse_headers($header);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = json_decode($body, true);
return $result;
}
How do I see the entire HTTP request in a plain text?
You can use Fiddler (http://www.telerik.com/fiddler) to see your complete request/response in plain text and to test your REST API as well.
Also, you can use Wireshark (https://www.wireshark.org/) applying a HTTP filter.
My recommendation is Fiddler, I use it a lot to test and debug my REST API.
Hope this helps

Problem when posting a file with curl in php

I'm trying to post a file with curl in php, but the file is never uploaded/accepted by the server. I have searched and tried for several hours, but I can't find whats wrong, everyone elses examples and codes seems to work, but not this one.
Here is the code:
<?php
$url = "http://jpptst.ams.se/0.52/default.aspx";
$headers = array(
"Content-Type: text/xml; charset=iso-8859-1",
"Accept: text/xml"
);
$data = array("file" => "#documents/xmls/1298634571.xml");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
The result I get:
string(904) "HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Mon, 25 Jul 2011 19:13:41 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 659"
Thats all I get.. the file is never accepted by the server.
If anyone can help me with this problem it would be much appreciated :)
Thanks!
You're trying to upload a file via HTTP post, so sending a Content-type: text/xml header is inappropriate. An HTTP file upload is actually done as multipart/form-data, and is actually pretty much identical to a MIME-encoded email attachment. PHP's curl will fill in the header details for you automatically. As well, the Accept header is not necessary either.
Check that the path to the .xml file you're trying to upload is correct. You've not specified a leading / to it, so the path is relative to where your PHP script is executing from.
Replace:
$data = array("file" => "#documents/xmls/1298634571.xml");
With this:
$data = array("file" => "#".realpath('documents/xmls/1298634571.xml'));
Try it, might work, i'm not sure tho.
EDIT:
Try this out:
<?php
$xmldatafile="documents/xmls/1298634571.xml"; // Make sure the file path is correct
function postData($postFields,$url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$postFileds);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$xmlData = file_get_contents($xmldatafile);
$postFileds = 'data='.$xmlData;
$result = postData($postFields,"http://jpptst.ams.se/0.52/default.aspx");
?>

Categories