I can call a soap server using java like this
Call call = new Call();
URL url = new URL("http://soap-something.dash.com/servlet/rpcrouter");
call.setTargetObjectURI("urn:login-transport");
call.setMethodName("confirmPassword");
call.setParams(a vector);
resp = call.invoke(url, "");
But my question is how can I call this same function using curl and php, I have already tried this, but it may be some kind of funny code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://soap-something.dash.com/servlet/rpcrouter?urn:login-transport");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "confirmPassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("username"=>"pritom", "password"=>"pritom"));
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "<br/>HTTP CODE: " . $httpCode;
print_r($head);
But it echo http code 100 and I do not found any result from soap server. But my soap server is ok, tested by java.
Looks fine to me, except CURLOPT_HEADER needs to be either true or false (include HTTP header in output or not),
curl_setopt($ch, CURLOPT_HEADER, "confirmPassword");
should be
curl_setopt($ch, CURLOPT_HEADER, true);
I can only guess what confirmPassword is, if it's a callback, you should use CURLOPT_HEADERFUNCTION instead.
But as Ariel pointed out, you're not telling us what the problem is.
Try removing this line:
curl_setopt($ch, CURLOPT_NOBODY, false); // remove body
Then post what the output is.
Related
I am using the following code to send a CURL request to send a SMS. But the SMS is being sent twice.
$message = urlencode($message);
$smsurl = "http://$url/sendmessage.php?user=matkaon&password=$password&mobile=$mobile&message=$message&sender=$sender&type=3";
$ch = curl_init($smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sentsms = curl_exec($ch);
curl_close($ch);
I tried commenting some of the lines which solved the issue but gives an output as below:
What is the proper method to send a CURL request only once?
Try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
Don't pass the URL as argument on the init function.
I don't know why the function is being called twice, but I never pass the URL as argument and always work great this way.
You'd usually use curl_init() with no parameters, then pass the URL into curl_exec.
Modified example 1 from curl_exec docs:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Description
I'm trying to make a request to an API via PHP cURL
$access_token = $tokens['access_token'];
$headers = array(
"Authorization: Bearer " . $access_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,env('USER_INFO'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1000);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_USERAGENT,'php');
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
$info = curl_getinfo($ch);
$userInfo = curl_exec($ch);
$userInfo = json_decode($userInfo, true);
dd($userInfo);
I kept getting this back
{"sub":"acr:123;type=STAT","updated_at":1509463516,"name":"User","email":"user#email.com"}
1
Try #2
If I do
$userInfo = json_encode($userInfo, true);
I got
{"sub":"acr:123;type=STAT","updated_at":1509463516,"name":"User","email":"user#email.com"}
"true"
How do I get rid of the 1 or true below it, and only get the JSON data?
Is there another param for json_decode() that I need to pass in?
Set the CURLOPT_RETURNTRANSFER curl option. curl_exec is just returning true without that, (it echoes the response and returns true; see curl_exec return values) and json_decode(true) is true.
How would one go about and debug this further?
A couple of things to experiment with:
Remove $userInfo = json_decode($userInfo, true); and
dd($userInfo);. Without the CURLOPT_RETURNTRANSFER option set, you should still see the non-decoded JSON, but not the true or 1.
Add another dd($userInfo); before the json_decode. You'll see what curl_exec actually returned, which may help you eliminate json_decode as a possible cause for this odd looking behavior.
The following line should solve your problem with the response.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
I am trying to send SMS from my localhost with xamp installed.
Requested page is on https and an .aspx page.
I am getting error: "HTTP Error 400. The request is badly formed." or blank page only in some cases.
Detaisl is as follows :
$url = 'https://www.ismartsms.net/iBulkSMS/HttpWS/SMSDynamicAPI.aspx';
$postArgs = 'UserId='.$username.
'&Password='.$password.
'&MobileNo='.$destination.
'&Message='.$text.
'&PushDateTime='.$PushDateTime.
'&Lang='.$Lang;
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$response = getSslPage($all);
echo "<pre>";
print_r($response); exit;
I tried every possible solution/combination found on internet but could not resolve that. The API developers do not have a example for php script.
I tried httpful php library and file_get_contents function but getting empty page. Also tried every combination with curl_setup.
I need to call this url without any post data and see the response from it.
Instead getting a blank page.
Please note that when I execute the url with all details in browser it works fine.
Can anybody help me in this regard.
Thank you,
Usman
First do urlencode over your data as follows:
$postArgs = 'UserId='. urlencode($username.
'&Password='.urlencode($password).
'&MobileNo='.urlencode($destination).
'&Message='.urlencode($text).
'&PushDateTime='.urlencode($PushDateTime).
'&Lang='.urlencode($Lang);
After that two possible solutions. One is using GET.
curl_setopt($ch, CURLOPT_URL, $url . "?" . $postArgs);
Second option is using POST method.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs);
I have a Affiliate URL Like http://track.abc.com/?affid=1234
open this link will go to http://www.abc.com
now i want to execute the http://track.abc.com/?affid=1234 Using CURL
and now how i can Get http://www.abc.com
with Curl ?
If you want cURL to follow redirect headers from the responses it receives, you need to set that option with:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
You may also want to limit the number of redirects it follows using:
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
So you'd using something similar to this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
Edit: Question wasn't exactly clear but from the comment below, if you want to get the redirect location, you need to get the headers from cURL and parse them for the Location header:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
This will give you the headers returned by the server in $data, simply parse through them to get the location header and you'll get your result. This question shows you how to do that.
I wrote a function that will extract any header from a cURL header response.
function getHeader($headerString, $key) {
preg_match('#\s\b' . $key . '\b:\s.*\s#', $headerString, $header);
return substr($header[0], strlen($key) + 3, -2);
}
In this case, you're looking for the value of the header Location. I tested the function by retrieving headers from a TinyURL, that redirects to http://google.se, using cURL.
$url = "http://tinyurl.com/dtrkv";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$location = getHeader($data, 'Location');
var_dump($location);
Output from the var_dump.
string(16) "http://google.se"
I'm having dificulties to query a webform using CURL with a PHP script. I suspect, that I'm sending something that the webserver does not like. In order to see what CURL realy sends I'd like to see the whole message that goes to the webserver.
How can I set-up CURL to give me the full output?
I did
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
but that onyl gives me a part of the header. The message content is not shown.
Thanks for all the answers! After all, they tell that It's not possible. I went down the road and got familiar with Wireshark. Not an easy task but definitely worth the effort.
Have you tried CURLINFO_HEADER_OUT?
Quoting the PHP manual for curl_getinfo:
CURLINFO_HEADER_OUT - The request string sent. For this to work, add
the CURLINFO_HEADER_OUT option to the handle by calling curl_setopt()
If you are wanting the content can't you just log it? I am doing something similar for my API calls
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::$apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, count($dataArray));
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$logger->info("Sending " . $dataString);
self::$results = curl_exec($ch);
curl_close($ch);
$decoded = json_decode(self::$results);
$logger->debug("Received " . serialize($decoded));
Or try
curl_setopt($ch, CURLOPT_STDERR, $fp);
I would recommend using curl_getinfo.
<?php
curl_exec($ch);
$info = curl_getinfo($ch);
if ( !empty($info) && is_array($info) {
print_r( $info );
} else {
throw new Exception('Curl Info is empty or not an array');
};
?>