I am sending a soap request using PHP curl(). I need to print my request, so that I can have a look into my request and understand weather it is going in a right format.
Here is my code:
$parameters = "<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ejb='http://ejb.gateway.ebpp.fawryis.com/'>
<soapenv:Header/>
<soapenv:Body>
<ejb:process>
//...
</ejb:process>
</soapenv:Body>
</soapenv:Envelope>";
$url='//URL to the service';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_ENCODING,'utf-8');
curl_setopt($curl,CURLOPT_HTTPHEADER,array (
'SOAPAction:""',
'Content-Type: text/xml;
charset=utf-8',
));
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $parameters);
$result = curl_exec($curl);
I get a wrong data sent error from the API side which means I am not sending a correct format.
Can anyone please let me know how to do that?
Update:
Verbose information
* About to connect() to 10.2.250.4 port 9081 (#0)
* Trying 10.2.250.4...
* connected
* Connected to 10.2.250.4 (10.2.250.4) port 9081 (#0)
> POST /CoreWeb/ApplicationBusinessFacadeService HTTP/1.1
Host: 10.2.250.4:9081
Accept: */*
Accept-Encoding: utf-8
SOAPAction:""
Content-Type: text/xml;
charset=utf-8
Content-Length: 1087
Expect: 100-continue
< HTTP/1.1 100 Continue
< Content-Length: 0
< Date: Thu, 20 Mar 2014 14:04:19 GMT
< Server: WebSphere Application Server/7.0
< HTTP/1.1 200 OK
< Date: Thu, 20 Mar 2014 14:04:19 GMT
< Server: WebSphere Application Server/7.0
< Content-Type: text/xml; charset=utf-8
< Content-Language: en-US
< Content-Length: 914
<
* Connection #0 to host 10.2.250.4 left intact
Your Header is broken into two lines:
'Content-Type: text/xml;
charset=utf-8',
Make it in one line. May be it is causing the problem for you.
'Content-Type: text/xml;charset=UTF-8',
UPDATE:
curl_setopt($curl,CURLOPT_HTTPHEADER,array (
'SOAPAction:""',
'Content-Type: text/xml;charset=utf-8',
'Expect:'
));
Related
curl works in CLI, but not in PHP.
The following command works in the command line:
curl -X POST --header "Content-Type: application/json" --header "Authorization: Basic [token]" https://api.example.com/v1/token -v -k
* About to connect() to api.example.com port 443 (#0)
..
> POST /v1/token HTTP/1.1
> User-Agent: curl/7.29.0
> Host: api.example.com
> Accept: */*
> Content-Type: application/json
> Authorization: Basic [token]
>
< HTTP/1.1 200 OK
< Date: Fri, 30 Apr 2021 02:52:24 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 325
< Connection: keep-alive
< X-Powered-By: Express
< ETag: W/"145-rseWkvhNxxhur+O7jUfApznKiww"
<
* Connection #0 to host api.example.com left intact
{"accesstoken":"token","type":"Bearer","expired":"20210501115224"}
And in PHP using the code below:
test.php
<?php
$host = 'https://api.example.com/v1/token';
$headers = array(
'Content-Type: application/json',
'Authorization: Basic [token]'
);
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $host);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($oCurl, CURLOPT_VERBOSE, true);
$response = curl_exec($oCurl);
curl_close($oCurl);
$ php test.php
* About to connect() to api.example.com port 443 (#0)
..
> POST /v1/token HTTP/1.1
Host: api.example.com
Accept: */*
Content-Type: application/json
Authorization: Basic [token]
Content-Length: -1
Expect: 100-continue
< HTTP/1.1 100 Continue
< HTTP/1.1 400 Bad Request
< Server: nginx
< Date: Fri, 30 Apr 2021 04:22:08 GMT
< Content-Type: text/html
< Content-Length: 150
< Connection: close
<
* Closing connection 0
The result is a HTTP Status code 400 Bad Request. Is there something I'm doing wrong?
Please Help.
Any ideas would be greatly appreciated.
Content-Length: -1 looks weird.
Seems like cURL is adding that automatically, because your request does not contain a POST body - but then it should be set to 0, if it gets set at all.
Add 'Content-Length: 0' to your $headers array, so that cURL won’t add the header itself with the wrong value.
#choi
did you encode your token?
ej.
$host = 'https://api.example.com/v1/token';
$token = base64_encode('username:Password0..');
$headers = array(
'Content-Type: application/json',
'Authorization: Basic ' . $token
);
$oCurl = curl_init();
curl_setopt($oCurl, CURLOPT_URL, $host);
...
Response
$> php curl_test.php
* Trying 127.0.0.1:8080...
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> POST /v1/token HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
Content-Type: application/json
Authorization: Basic dXNlcm5hbWU6UGFzt3dtcmQwLi4=
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx
< Date: Fri, 30 Apr 2021 06:50:59 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
...
Ref: curl basic auth
I'm trying to make a request to payment processing page. This requires authorization, which takes place through a set of redirects. In the second step, I get "411 Length Required" error, which means that content-length was lost along the way. Indeed, I cannot see it in the log. What can be done here? Change tool (programming language)?
CURLOPT_VERBOSE:
* Trying xxx.xxx.xxx.xxx...
* TCP_NODELAY set
* Connected to api.dev.example.com (188.186.236.44) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: OU=Domain Control Validated; OU=PositiveSSL Wildcard; CN=*.dev.example.com
* start date: Apr 27 00:00:00 2019 GMT
* expire date: Apr 26 23:59:59 2021 GMT
* subjectAltName: host "api.dev.example.com" matched cert's "*.dev.example.com"
* issuer: C=GB; ST=Greater Manchester; L=Salford; O=Sectigo Limited; CN=Sectigo RSA Domain Validation Secure Server CA
* SSL certificate verify ok.
> POST /p2p/v2/payer HTTP/1.1
Host: api.dev.example.com
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Content-Length: 224
* upload completely sent off: 224 out of 224 bytes
< HTTP/1.1 302 Found
< Server: nginx
< Date: Mon, 13 Jul 2020 14:22:54 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 213
< Connection: keep-alive
< Keep-Alive: timeout=20
< Cache-Control: private
< Location: /api/payer/auth?sessionToken=e744a95992fa405ba10662bbc6908d6bedd48a73cc0d45d589f4ef2f7d7a0b88
< Set-Cookie: returnUrl=http://example.com/returnurl.php; path=/
<
* Ignoring the response-body
* Connection #0 to host api.dev.walletone.com left intact
* Issue another request to this URL: 'https://api.dev.example.com/auth?sessionToken=e744b95992fa405ba10662bbc6908d6b7dd48a73cc0d45d589f4ef2f7d7a0b88'
* Switch from POST to GET
* Found bundle for host api.dev.example.com: 0x5649fd243480 [can pipeline]
* Re-using existing connection! (#0) with host api.dev.example.com
* Connected to api.dev.example.com (188.186.236.44) port 443 (#0)
> POST /auth?sessionToken=e744b95992fa405ba10662bbc6908d6b7dd48a73cc0d45d589f4ef2f7d7a0b88 HTTP/1.1
Host: api.dev.example.com
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
< HTTP/1.1 411 Length Required
< Server: nginx
< Date: Mon, 13 Jul 2020 14:22:54 GMT
< Content-Type: text/html; charset=us-ascii
< Content-Length: 344
< Connection: keep-alive
< Keep-Alive: timeout=20
<
* Connection #0 to host api.dev.example.com left intact
My code is:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array (
"Content-Type: application/x-www-form-urlencoded",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $curl_method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $order_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $verbose);
$response = curl_exec($ch);
curl_close($ch);
Set the content-length in the header, which would be set to the string length strlen() of $order_data
curl_setopt($ch, CURLOPT_HTTPHEADER, Array (
"Content-Type: application/x-www-form-urlencoded",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Content-Length: ". strlen($order_data)
));
you can also debug this by checking out curl_setopt($ch, CURLINFO_HEADER_OUT, true); which makes curl_getinfo() include the request's headers in its output.
The problem was in using curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $curl_method). Curl tryed to swithc to GET, like mostly browsers do, but cannot. Use curl_setopt($ch, CURLOPT_POST, 1); indeed.
I have a strange situation.
I am trying to make curl requests in php. Sometimes it returns valid response, sometimes it returns "HTTP/1.1 401 Unauthorized. One common pattern is, if I wait for long time, 5 minutes between requests it works. If I make a request immediately after each other it fails. (This pattern is not consistent either) But sometimes I can make successful request one after another.
If I make the same request using curl command it works all the time. The verbose between the two are exactly the same.
<?php
$api = new ApiRestTest(URL,LOGIN,APIKEY);
$result = $api->curl_req(CONTACT_TEST);
var_dump($result);
class ApiRestTest
{
protected $_url;
protected $_username;
protected $_apiKey;
public function __construct($url, $username, $apiUserKey) {
$this->_url = $url;
$this->_username = $username;
$this->_apiKey = $apiUserKey;
}
private function getHeader() {
$nonce = base64_encode(substr(md5(uniqid()), 0, 16));;
$created = date('c');
$digest = base64_encode(sha1(base64_decode($nonce) . $created . $this->_apiKey, true));
$wsseHeader[] = "Content-type:application/vnd.api+json";
$wsseHeader[] = "Accept: application/json";
$wsseHeader[] = "Authorization: WSSE profile=\"UsernameToken\"";
$wsseHeader[]= sprintf(
'X-WSSE: UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"', $this->_username, $digest, $nonce, $created
);
var_dump($wsseHeader);
return $wsseHeader;
}
public function curl_req($path, $data=array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->_url . $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/7.54.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->getHeader());
$data = curl_exec($ch);
curl_close($ch);
return $data;
}}
curl command line output
$ curl -v -H "Content-type:application/vnd.api+json" -H "Accept: application/json" -H 'Authorization: WSSE profile="UsernameToken"' -H 'X-WSSE: UsernameToken Username="system", PasswordDigest="D1jXzXaTxcZDJ6YKvtqkggkhXV8=", Nonce="ODM3ZWVmZTRiY2U0MmIzNQ==", Created="2017-08-30T10:16:55+10:00"' http://orocampus.tk/app.php/api/contacts/2
Trying 45.76.122.100...
TCP_NODELAY set
Connected to orocampus.tk (45.76.122.100) port 80 (#0)
> GET /app.php/api/contacts/2 HTTP/1.1
> Host: orocampus.tk
> User-Agent: curl/7.54.0
> Content-type:application/vnd.api+json
> Accept: application/json
> Authorization: WSSE profile="UsernameToken"
> X-WSSE: UsernameToken Username="system", PasswordDigest="D1jXzXaTxcZDJ6YKvtqkggkhXV8=", Nonce="ODM3ZWVmZTRiY2U0MmIzNQ==", Created="2017-08-30T10:16:55+10:00"
>
< HTTP/1.1 200 OK
< Server: nginx/1.13.3
< Date: Wed, 30 Aug 2017 00:17:17 GMT
< Content-Type: application/vnd.api+json
< Transfer-Encoding: chunked
< Cache-Control: no-cache
< Set-Cookie: SERVERID=web2; path=/
<
* Connection #0 to host orocampus.tk left intact
php output
$ php test.php
array(4) {
[0]=>
string(37) "Content-type:application/vnd.api+json"
[1]=>
string(24) "Accept: application/json"
[2]=>
string(43) "Authorization: WSSE profile="UsernameToken""
[3]=>
string(157) "X-WSSE: UsernameToken Username="system", PasswordDigest="D1jXzXaTxcZDJ6YKvtqkggkhXV8=", Nonce="ODM3ZWVmZTRiY2U0MmIzNQ==", Created="2017-08-30T10:16:55+10:00""
}
* Trying 45.76.122.100...
* TCP_NODELAY set
* Connected to orocampus.tk (45.76.122.100) port 80 (#0)
> GET /app.php/api/contacts/2 HTTP/1.1
Host: orocampus.tk
User-Agent: curl/7.54.0
Content-type:application/vnd.api+json
Accept: application/json
Authorization: WSSE profile="UsernameToken"
X-WSSE: UsernameToken Username="system", PasswordDigest="D1jXzXaTxcZDJ6YKvtqkggkhXV8=", Nonce="ODM3ZWVmZTRiY2U0MmIzNQ==", Created="2017-08-30T10:16:55+10:00"
< HTTP/1.1 401 Unauthorized
< Server: nginx/1.13.3
< Date: Wed, 30 Aug 2017 00:16:53 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Cache-Control: no-cache
< WWW-Authenticate: WSSE realm="Secured API", profile="UsernameToken"
< Set-Cookie: SERVERID=web2; path=/
<
* Connection #0 to host orocampus.tk left intact
string(277) "HTTP/1.1 401 Unauthorized
Server: nginx/1.13.3
Date: Wed, 30 Aug 2017 00:16:53 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Cache-Control: no-cache
WWW-Authenticate: WSSE realm="Secured API", profile="UsernameToken"
Set-Cookie: SERVERID=web2; path=/
I have tried to run curl in php. It also failed
function request($path) {
$curl = "curl -v -H \"Content-type:application/vnd.api+json\" -H \"Accept: application/json\" -H 'Authorization: WSSE profile=\"UsernameToken\"'".
" -H ".$this->getHeader()[3]."' " . $this->_url . $path;
var_dump($curl);
exec($curl, $output, $exit);
var_dump($output);
return $exit == 0;
}
If I run the same curl command in the terminal, it works fine. What is the problem?
In curl default content type not changing 'application/json' in output.
$headers = array();
$headers[] = 'Accept: application/xml';
$headers[] = 'Content-Type: application/xml';
$url = url('someurl here');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response=curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Output:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Request-Time: 295
Date: Wed, 28 Jun 2017 07:41:58 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Connection: Transfer-Encoding
am i done any mistake in that above code? I need output in xml format only.
There is no mistake. Content-type header that you setted is a request header. It defines type of content that you're sending to a server. Which make no sense in GET requests type.
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
...
Is a response headers. Content type here shows format of the server response.
Maybe you didn't pass some parameter that tells server to return xml data instead of json.
I am trying to create a Freshdesk ticket using freshdesk API in php curl.
followed this documentation link https://github.com/freshdesk/fresh-samples/tree/master/php_samples
But it is not creating ticket
<?php
$fd_domain = "http://test.freshdesk.com";
$token = "apikey";
$password = "xxx";
$email = "aaa#bbb.com";
$data = array(
"helpdesk_ticket" => array(
"description" => "Some details on the issue ...",
"subject" => "TEST Support needed..",
"email" => "tom#outerspace.com",
"priority" => 1,
"status" => 2
),
"cc_emails" => "ram#freshdesk.com,diana#freshdesk.com"
);
$json_body = json_encode($data, JSON_FORCE_OBJECT | JSON_PRETTY_PRINT);
$header[] = "Content-type: application/json";
$connection = curl_init("$fd_domain/helpdesk/tickets.json");
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($connection, CURLOPT_HTTPHEADER, $header);
curl_setopt($connection, CURLOPT_HEADER, false);
curl_setopt($connection, CURLOPT_USERPWD, "$email:$password");
curl_setopt($connection, CURLOPT_POST, true);
curl_setopt($connection, CURLOPT_POSTFIELDS, $json_body);
curl_setopt($connection, CURLOPT_VERBOSE, 1);
$response = curl_exec($connection);
echo $response;
?>
when i run this file in terminal as php file.php But ticket is not running, I am getting the following console results
But I am not sure what is happening.
* Hostname was NOT found in DNS cache
* Trying 107.22.197.253...
* Connected to test.freshdesk.com (107.22.197.253) port 80 (#0)
* Server auth using Basic with user 'aaa#bbb.com'
> POST /helpdesk/tickets.json HTTP/1.1
Authorization: Basic bW9oYW5Ad2h5YWJsZS5jb206bW9oYW4xMjM=
Host: test.freshdesk.com
Accept: */*
Content-type: application/json
Content-Length: 274
* upload completely sent off: 274 out of 274 bytes
< HTTP/1.1 302 Found
< Cache-Control: no-cache
< Content-Type: text/html; charset=utf-8
< Location: https://test.freshdesk.com/helpdesk/tickets.json
< Set-Cookie: helpdesk_node_session=0fbdb53a62fff9b78f57d069a7abfde86bef1e16eefa8710dbcadb99a1723a3a42affa906dd23ceb6e77f20f0a0e703a2cb6d3423cb94ec074f0006dadfe4b02; path=/
< Set-Cookie: _helpkit_session=BAh7CDoPc2Vzc2lvbl9pZCIlMzdmNGI4ZDZkZWMxNmRhNDBkYWI1ZTFiODU2NDFkODhJIhV1c2VyX2NyZWRlbnRpYWxzBjoGRUZJIgGAZmYxMmExZWJjZGFiMTlkMjgxYjlkYmI0NjIwZjdhMjFlYTgxM2YyZWE5M2UzYTk0MTlhYzBhNDA2M2QyMzEwNTdmNDQ4NjVjYmE1MjA1ZjU2ODUzNGI2ZGQ4NTExZjljZGUyZGQxNzViMzNjMzhkZTYxMTc1MDRkMWE2MGMyOTEGOwZUSSIYdXNlcl9jcmVkZW50aWFsc19pZAY7BkZsKwj%2B0TIqAQA%3D--fdc65abdf0bdcf0c38db4837f05b19050ef1a2f0; path=/; HttpOnly
< Status: 302
< X-Runtime: 25
< Content-Length: 117
< Connection: keep-alive
<
* Connection #0 to host test.freshdesk.com left intact
<html><body>You are being redirected.</body></html>
please some help me