PHP cURL Json Post - doing something wrong - php

I'm new to php and I'm trying to create a simple example for calling our company api. I got NetBeans IDE 7.1.2 to work last night (yay) but I cannot seem to get the following code to show me anything. I can run it in the debugger. I can step through it. I even get to the end without errors, but the curl_exec returns just 0. I have added the CURLOPT_PROXYPORT so that I can get fiddler to see the traffic, but fiddler sees nothing. I am also trying to run this as a php command line (if that has any bearing).
I know I'm doing something stupid... but that's the problem with stupid.
<?php
$url = 'https://target.boomerang.com/api/JobCreate';
$authToken = 'phptest';
$data = array(
"emailHTML" => "Howdy",
"jobKind" => "email",
"senderEmail" => "bob#boomerang.com",
"subject" => "My howdy email"
);
$data_string = json_encode($data);
$headers = array(
'Content-type: application/json',
'auth_token: ' . $authToken,
'Accept: application/json',
'Expect:'
);
$ch = curl_init();
$args = array(
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_PROXYPORT => "localhost:8888"
);
curl_setopt_array($ch, $args);
$res = curl_exec($ch);
$res_data = json_decode($res, true);
print($res_data);
?>
Thanks for any help.

So the problem was caused by SSL certificate mismatch? I suppose it can be solved by adding this...
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
...into $args array.

Related

problems getting curl / json work with php

I just wanted to ask one of my servers (let's call him mysql-server) a simple question to get some result out of a MySQL database.
This mysql-server provides the result as json decoded array in the form - e.g. like this:
$data = array("first_name" => "First name","last_name" => "last name");
$data_string = json_encode(array("customer" => $data));
echo $data_string;
which works pretty well and returns something like this:
{"customer":{"first_name":"First name","last_name":"last name"}}
Just to check that everything is fine I added a few lines:
echo "<pre>";
print_r(json_decode($data_string, true))."</pre>";
and got the following:
Array(
[customer] => Array
(
[first_name] => First name
[last_name] => last name
)
)
So everything seems to be fine on this server side.
If I now call the url of that script from the second-server using:
$data_string = json_encode(array('customer' => $customerNo));
$curlHandler = curl_init($url);
curl_setopt_array($curlHandler, [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_STDERR => fopen($fp, 'w'),
//CURLOPT_SSL_VERIFYHOST == 2,
//CURLOPT_VERIFYPEER == true,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
]);
$response = curl_exec($curlHandler);
var_dump(json_decode($response, true));
I got NULL.
Instead of using
echo $response;
brings the following result to the screen:
{"customer":{"first_name":"First name","last_name":"last name"}}
and a
var_dump($response);
brings the following to the screen:
string(268) " {"customer":{"first_name":"First name","last_name":"last name"}} "
So the data is transfered back and stored in the variable $response as it should be, but may anyone give me please a hint, why I can't access it using json_decode?
Many thanks and best
Paul

Twilio Verify API: "Requested URL was not found"

I am using curl to perform a request to the Twilio Verify API, following the instructions here: https://www.twilio.com/verify/api
Using these instructions, I've created two php files to perform the curl request -- one to get a verification code (get_code.php), and another to check the verification code (check_code.php). These scripts are called using an ajax post to send the parameters, and the two scripts are nearly identical, save for the URL ("/start" vs. "/check").
I believe I am specifying the correct URLs, and get_code.php works, but check_code.php throws the following error:
Requested URL was not found. Please check http://docs.authy.com/ to see the valid URLs.
get_code.php
<?php
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://api.authy.com/protected/json/phones/verification/start",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'country_code' => '1',
'via' => 'sms',
'phone_number' => $USER_PHONE,
),
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
check_code.php
<?php
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$VERIFY_CODE = htmlspecialchars($_POST["code"]);
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "https://api.authy.com/protected/json/phones/verification/check",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'country_code' => '1',
'phone_number' => $USER_PHONE,
'verification_code' => $VERIFY_CODE
),
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
I performed a curl manually in terminal using the same URL and parameters, and it worked.
curl "https://api.authy.com/protected/json/phones/verification/check?phone_number=MY_PHONE&country_code=1&verification_code=MY_CODE" -H "X-Authy-API-Key: MY_KEY"
I don't know what I could be doing wrong?
OK, well I have no idea why this worked, but I got it working and maybe someone else can explain why. I built the CURL URL as a string and I removed the CURLOPT_RETURNTRANSFER and CURLOPT_POST arguments.
<?php
$USER_COUNTRY = "1";
$USER_PHONE = htmlspecialchars($_POST["phone"]);
$VERIFY_CODE = htmlspecialchars($_POST["code"]);
$URL = "https://api.authy.com/protected/json/phones/verification/check?country_code=1&phone_number=".$USER_PHONE."&verification_code=".$VERIFY_CODE;
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => $URL,
CURLOPT_HTTPHEADER => array('X-Authy-API-Key: MY_KEY')
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
🤷

VB to PHP Curl posting xml

I am trying to convert this VB script to PHP curl
xmlServerHttp.open "POST","url",False xmlServerHttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlServerHttp.send "xmlmessage=" & Server.URLEncode(xmlDocument)
‘ xmlDocument = the Xml Document contain the actual request
xmlServerStatus = xmlServerHttp.status
if xmlServerStatus = "200" then
xmlServerResponse = xmlServerHttp.responseText
Else
Response.Appendtolog ".xmlServer status is " & xmlServerStatus
end if
This is what I have so far however it is failing
$curl = curl_init(url);
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded') ,
CURLOPT_POSTFIELDS => $xmldoc,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
);
// Setting curl options
curl_setopt_array( $curl, $options );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
// Getting results
echo curl_exec($curl);
The API i am calling return that the xmlmessage variable is not a valid xml document.
try to change $xmlDoc to $xmlDoc->asXML()
like this
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded') ,
CURLOPT_POSTFIELDS => array('xmlmessage='=> $xmlDoc),
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
);

CURL getting error:14094410:SSL

This is my code. Am trying get information from a server.
But I got stuck in the middle. Please help me.
Am getting error such as
[errors] => Array ( [0] => error:14094410:SSL
routines:SSL3_READ_BYTES:sslv3 alert handshake failure ) )
Below is the code that I have written please check and help me.
Will appreciate any help.
public function get_quotes(){
$reservation_obj->source = "test";
$reservation_obj->location_code = "test";
$reservation_obj->start_date = "2010-06-01 19:00";
$reservation_obj->end_date = "2010-06-04 13:50";
$reservation_obj->action = "get_quotes";
$json = json_encode($reservation_obj);
$curl_opts = array(
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => array('Content-Type: text/json', 'Content-length: '.strlen($json)),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $json,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => "https://myserver/test.php",
CURLOPT_VERBOSE => false,
CURLOPT_SSLCERT => getcwd()."/newfile.crt.pem",
CURLOPT_SSLCERTTYPE => "PEM",
CURLOPT_SSLKEY=> getcwd()."/newfile.key.pem",
CURLOPT_SSLKEYTYPE => "PEM",
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSLVERSION => 3
);
$curl = curl_init();
curl_setopt_array($curl, $curl_opts);
if(!$response = curl_exec($curl)){
$response->errors[] = curl_error($curl);
}
curl_close($curl);
if(count($response ->errors)){
print_r($response);
}else{
print_r($response);
}
}
comment out the CURLOPT_SSLVERSION => 3 line and try again.
They say the SSL v3 is vulnerable (idk much about that, sorry), so many of the servers do not allow it using the v3.
Here is an article if you are interested. https://access.redhat.com/articles/1232123
Please let me know the result after you try it. thanks.

Sending a POST with PHP doesn't work with curl or file_get_contents, just normal bash CURL

I'm having some serious problems sending a POST. Using curl on the shell, my POST works perfectly. However, when using PHP curl, or file_get_contents, it doesn't work at all. I get a 500 error from the webserver.
curl -X POST -H"Content-Type:application/xml" "http://myserver:8080/createItem?name=NewItem" --user root:123456 --data-binary #template.xml
And this:
$options = array(
CURLOPT_HEADER => 1,
CURLOPT_HTTPHEADER => array("Content-Type:application/xml"),
CURLOPT_URL => "http://myserver:8080/createItem?name=" . rawurlencode("NewItem"),
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 20,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "root:123456",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => file_get_contents("template.xml"),
);
$post = curl_init();
curl_setopt_array($post, $options);
if(!$result = curl_exec($post)) {
trigger_error(curl_error($post));
}
curl_close($post);
And this:
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode('root:123456')) . "Content-Type:application/xml",
'timeout' => 20,
'content' => file_get_contents("template.xml"),
),
));
$ret = file_get_contents("http://myserver:8080/createItem?name=" . rawurlencode("NewItem"), false, $context);
Am i doing something absurd here and i'm not seeing? I don't see a reason for the normal curl from the shell to work perfectly, but not the PHP implementations.
Hum... I don't think that's possible with php/curl, but try:
CURLOPT_POSTFIELDS => array('#template.xml'),
It IS possible, take a look at this:
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
Source: http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
According to the documentation, headers here
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode('root:123456'))
. "Content-Type:application/xml",
should end with \r\n [see example 1 here]
'header' => sprintf( "Authorization: Basic %s\r\n", base64_encode('root:123456'))
. "Content-Type: application/xml\r\n",

Categories