PHP curl is not parsing POST data - php

So i am trying to send a HTTPS POST request to a website but curl is not setting my post data nor my headers.
Here is my code(i changed the domain and values):
<?php
//Create connection
$ch = curl_init();
//Set the headers
$headers = array();
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: website.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Something: Something_Data";
//Set the POST data
$headers = array();
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: website.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Something: Something_Data";
// Configure the request (HTTPS)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, "https://website.com/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "MyUserAgentHere");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1); //just to be sure...
//Set the POST data and Headers
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("DATA1=VAL1&DATA2=VAL2"));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);
echo $result;
curl_close($ch);
?>
So basically i'm trying to send a HTTPS POST request to https://website.com/test". The request is sent and i get an answer but for some reasons it doesn't parse the POST data nor the Headers.
Here is my request in fiddler:
POST https://website.com/test HTTP/1.1
Host: website.com
Here is the answer from the request:
HTTP/1.1 400 Bad Request
Date: Fri, 06 Nov 2015 00:57:15 GMT
Server: Apache
Cache-Control: no-store
Pragma: no-cache
Connection: close
Content-Type: application/json;charset=UTF-8
Content-Length: 158
{"error":"unsupported DATA1"}
As you can see, the POST data and the headers are not set in my request for some reason. I have no idea why curl is not setting my headers and my post fields.
Infos: I am using XAMPP with php5.

You shouldn't URL-encode the entire parameter string, that will encode the = and & so they're no longer delimiters. You should just encode the values.
curl_setopt($ch, CURLOPT_POSTFIELDS, 'DATA1=' . urlencode($val1) . '&DATA2' . urlencode($val2));
You can use an array instead, and cURL will do the encoding for you.
curl_setopt($ch, CURLOPT_POSTFIELDS, array('DATA1' => $val1, 'DATA2' => $val2));
If you use the array mechanism, remove the Content-type: application/x-www-form-urlencoded header, because cURL sends it in multipart/form-data format.

Related

PHP cURL not sending headers (?)

I attempted to modify the code I found on this webpage: https://tutorialsclass.com/php-rest-api-get-data-using-curl/
The API I'm using needs two custom headers: Accept-Encodig and x-api-key.
My modified code looks like this:
$curl_handle = curl_init();
$url = "http://thelinktomyAPI";
$headers = array(
'Accept-Encoding: application/json',
'Content-Type: application/json',
'x-api-key: MyLongAPIKey'
);
// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_ENCODING, 'application/json');
// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$curl_data = curl_exec($curl_handle);
if ($curl_data === false)
{
print_r('Curl error: ' . curl_error($curl_handle));
}
curl_close($curl_handle);
print_r($curl_data);
So as you can see I'm making my headers visible to make sure I'm actually sending them. What I get shown is a webpage with the following:
HTTP/1.1 302 Moved Temporarily Date: Thu, 02 Dec 2021 08:09:59 GMT
Content-Type: text/html Content-Length: 110
Connection: keep-alive
Location: http://thelinktomyAPI
X-Trace-ID:f306fe34-079b-493c-b5db-073e792ec2a1
X-Kong-Response-Latency: 0
Server: kong/2.3.3 302 Found
Since the Content-Type is shown as "text/html" it leads me to believe that my headers aren't sent at all, hence I'm not getting any data. I'd appreciate any help.
Edit Adding curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); as Professor Abronsius suggested helped!

POST SOAP Web-Services with PHP cURL (moving from SOAPUI)

I am totally stuck in SOAP integration. I have searched and continously searching for ways how to make it right. My system needs to connect to a certain web service. the URL redirects me to a ?wsdl page.
I am using SOAPUI to submit request, it is giving me the correct response. I also used SOAPClient but to no avail. I think this is no longer commonly used to this day. I can't find what my error is. Anyone can enlighten me with this? Thank you in advanced.
The SOAPUI request looks like this. It gives me the correct reponse:
POST https://example.com/blah/blah/Upload_v1_Port HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="upload_v1_Binder_ping"
Content-Length: 205
Host: bm-webservices-test.example.com.au
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Cookie: TS01128e1b=01c05e74c88cb127b6ba1e3cd907936f06957fdeead8e6f308ed6281d48b5da0dbb51b6ca0
Cookie2: $Version=1
Authorization: Basic <username:password>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:dir="http://test.com/mrb/ws/upload">
<soap:Header/>
<soap:Body>
<dir:ping/>
</soap:Body>
</soap:Envelope>
and this is my PHP Written code:
$credentials = "username:password";
$headers = array(
"POST https://example.com/blah/blah/Upload_v1_Port HTTP/1.1",
"Accept-Encoding: gzip,deflate",
"Content-Type: application/soap+xml;charset=UTF-8;action=upload_v1_Binder_ping",
"Content-Length: 205",
"Host: bfs-ws-test.test.com.au",
"Connection: Keep-Alive",
"User-Agent: Apache-HttpClient/4.1.1 (java 1.5)",
"Cookie: TS01128e1b=01c05e74c88cb127b6ba1e3cd907936f06957fdeead8e6f308ed6281d48b5da0dbb51b6ca0",
"Cookie2: $Version=1",
"Authorization: Basic $credentials",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.test.com/upload');
curl_setopt($ch, CURLOPT_CERTINFO, CACERT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_file);
$data = curl_exec($ch);
//pre_print_r($data); exit();
pre_print_r(curl_getinfo($ch));
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
echo 'test';
pre_print_r($data);
curl_close($ch);
}
And this is the result that I am getting:
</pre><hr>test<pre>--MIMEBoundary_ceea4229e38f12b7cd400440c7bdc1c1fe376164e3efa031
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.deea4229e38f12b7cd400440c7bdc1c1fe376164e3efa031#apache.org>
<faultstring>[ISS.0088.9171] Operation not found for soapAction = null</faultstring>
Solution :
it might caused by a wrong Content-Type in the HTTP POST. Setting text/xml in header may solve the problem.
Well you have 2 syntax errors:
$headers = array(
"POST https://example.com/blah/blah/Upload_v1_Port HTTP/1.1",
Accept-Encoding: gzip,deflate //no quotes no comma
"Content-Type: application/soap+xml;charset=UTF-8;action="upload_v1_Binder_ping", //3 qootes.
"Content-Length: 205",
"Host: bfs-ws-test.test.com.au",
"Connection: Keep-Alive",
"User-Agent: Apache-HttpClient/4.1.1 (java 1.5)",
"Cookie: TS01128e1b=01c05e74c88cb127b6ba1e3cd907936f06957fdeead8e6f308ed6281d48b5da0dbb51b6ca0",
"Cookie2: $Version=1",
"Authorization: Basic $credentials",
);
Or is it just a typo in the question.

Post a link in VIADEO using it API

I have tried to post a link in viadeo net work using it API, i have used the following CURL code. but its not working, I have tried with php as my server side code. Can Any one please help me to sort out this.
API reference
$url = 'https://partners.viadeo.com/api/member/activity/share';
$headers[] = 'Authorization: Bearer ' . $userArray['accessToken'];
$headers[] = "Accept: language/json";
$headers[] = 'Content-Type: application/JSON';
$headers[] = 'X-CSRF-Token: ' . $token;
$fields = array(
'status' => "My Messafge",
'url' => "www.google.com"
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
But I got response as below
HTTP/1.1 406 Not Acceptable
Cache-control: no-cache="set-cookie"
Content-Type: application/json; charset=utf-8
Date: Wed, 10 Aug 2016 09:20:16 GMT
ETag: W/"79-LjiXR3TEaswtDc0NbPbnlA"
Server: nginx/1.8.1
Set-Cookie: AWSELB=EB652D71104D5A540BBD90F6B05BD98BEE3F41DAFDC50D85BE8FBFFCF095D7D78EB45C92813C3646048981782B8C4C76C884420B93A5B55954F394AA61798FEFC028CD93F8;PATH=/
X-Powered-By: Express
Content-Length: 121
Connection: keep-alive
{"error":"The provided Accept header is not acceptable. Try one of the following : application/json,multipart/form-data"}
Try Like this, it will work for sure
$headers[] = "Accept: application/json";
$headers[] = 'Content-Type: application/json';

Php CURL Though I set the header as application/json the request goes as application/x-www-form-urlencoded

I am trying to POST JSON in my code.Though I set the header Content_Type:application/json the receiving server gets as Content-Type: application/x-www-form-urlencoded
$Content_Type = "application/json";
$header_info = "X_RESTBUS_MESSAGE_ID:".$X_RESTBUS_MESSAGE_ID.","."X_BU_ID:".$X_BU_ID.","."Content_Type:".$Content_Type;
$url = $server_url;
$content = json_encode($body_data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array($header_info));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
but the server receiving the POST call gets as
13 > X_RESTBUS_MESSAGE_ID: <aaaaa>,X_BU_ID:<xxxx>,Content_Type:application/json
13 > Accept: */*
13 > Content-Type: application/x-www-form-urlencoded
13 > Content-Length: 641
13 > Host: localhost:49111
13 >
You've CURLOPT_HTTPHEADER to be an array with a single item in it, which is a string or comma separated headers.
You should set it to be an associative array where each key/value pair is a single header.
You also need to spell the header name correctly. Content-Type has a hyphen in the middle, not an underscore.
$headers = array(
"X_RESTBUS_MESSAGE_ID" => $X_RESTBUS_MESSAGE_ID,
"X_BU_ID" => $X_BU_ID,
"Content-Type" => $Content_Type
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

Handling HTTP headers sent with 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);

Categories