I am having trouble converting an array into xml and then posting it to an third party URL as an xml post.
I believe i am close but i am missing something for it to work.
I am using wordpress and gravity form (I don't think it matters)
Here is what i have up-till now.
function post_to_third_party($entry, $form) {
$post_url = 'https://xxxx.com/home/BorrowerImport.do?CampaignID=xxx';
$body = array( 'firstname' => $entry['8.3'],
'lastname' => $entry['8.6'],
'dayphone' => $entry['12'],
'email' => $entry['11']
);
$xml = new SimpleXMLElement('<application/>');
$body = array_flip($body);
array_walk($body, array ($xml, 'addChild'));
print $xml->asXML();
$ch = curl_init($url);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
}
I also tried the following code this seems to work but the var_dump looks like string(201) " $firstname $lastname $dayphone $email " ok
I don't know how to populate the xml tags with the data gathered from the $body array
Here is the code i used for this result
add_action('gform_after_submission', 'post_to_third_party', 10, 2);
function post_to_third_party($entry, $form) {
$post_url = 'https://xxxx.com/home/BorrowerImport.do?CampaignID=xxx';
$body = array(
'firstname' => $entry['8.3'],
'lastname' => $entry['8.6'],
'dayphone' => $entry['12'],
'email' => $entry['11']
);
$xml = '
<?xml version="1.0" encoding="UTF-8"?>
<application>
<firstname>$firstname</firstname>
<lastname>$lastname</lastname>
<dayphone>$dayphone</dayphone>
<email>$email</email>
</application>';
var_dump($xml);
$ch = curl_init($url);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
}
Ok, here's the problem:
The method you are using to build the array before converting to XML is accidentally deleting some of the data.
You start out with something like this:
$body = array(
"firstname" => "Test",
"lastname" => "Test",
"dayphone" => "(801)735-2222",
"email" => "test#gmail.com",
);
Then you call array_flip, which gives you this:
$body = array(
"Test" => "firstname",
"Test" => "lastname",
"(801)735-2222" => "dayphone",
"test#gmail.com" => "email",
);
You now have two "Test" keys in your array. PHP keeps the last one an throws away the first. So your XML document is now missing one of the required keys: "firstname".
The solution is to build your XML document the old fashioned way and stay away from fancy stuff like array_flip:
foreach($body as $k=$v)
$xml->addChild($k, $v);
Related
Essentially the following script is working fine when in local host but fails to load (Timeout) anytime loaded from a web server, is their something incorrectly written? I know it's not the end point api url that's the issue, since it works on local. Please see below, wondering if it's caused by curl_setopt:
$id = "dhl";
$number = "039103913";
// Parameters setting
$key = 'key';
$secret = 'scret';
$param = array (
'id' => $id,
'number' => $number,
'phone' => '',
'ship_from' => '',
'ship_to' => '',
'area_show' => 1,
'order' => 'desc'
);
// Request Json
$json = json_encode($param, JSON_UNESCAPED_UNICODE);
$signature = strtoupper(md5($json.$key.$secret));
$url = 'https://www.kd100.com/api/v1/tracking/realtime';
$headers = array (
'Content-Type:application/json',
'API-Key:'.$key,
'signature:'.$signature,
'Content-Length:'.strlen($json)
);
// Send post request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$data = json_decode($result, true);
echo json_encode($data);
I am using a CURL post to an existing API. The API returns the error content must be JSON or plain text. I json_encode data and post the encoded data to the API.
In the API, it says the Content-Type should be application/x-www-form-urlencoded'.
However, it still returns the error content must be JSON or plain text. What could be the issue with my code below after submitting my data in JSON format?
This is a sample request body in the API doc.
"item" : "Store",
"content" : {
"channel":"false",
"hop": "false",
"msg": "Order successfully placed."
}
Controller
public function postUrl()
{
$url = "https://api.com/";
$data = array("item" => "Nike Shoes","content" => array ("channel" => "false" ,"hop" => "false","msg" => "Item Sold"));
$postdata = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postdata);
}
Perhaps this is because boolean values are specified as strings.
public function postUrl()
{
$url = 'https://api.com/';
$data = [
'item' => 'Nike Shoes',
'content' => [
'channel' => false,
'hop' => false,
'msg' => 'Item Sold'
]
];
$postData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postData);
}
Also try this
public function postUrl()
{
$url = 'https://api.com/';
$data = [
'item' => 'Nike Shoes',
'content' => [
'channel' => 'false',
'hop' => 'false',
'msg' => 'Item Sold'
]
];
$postData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/json']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
curl_close($ch);
Log::info($postData);
}
I am using ci framework, and i try to create notification using
onesignal API. I have hosted the website using https. But why my JSON
received is always false using onesignal API, but if i test using
postman it runs fine without error.
This is my code
function send_message($id, $nama) {
$content = array(
"en" => $nama
);
$heading =array(
"en" => $id
);
$fields = array(
'app_id' => "2cd5ad24-a2d9-4e7d-ac66-7494cebd8e84",
'included_segments' => array(
'Active Users'
),
'contents' => $content,
'headings' => $heading,
'url' => "https://esop.matahariled.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic MDQ0NjY0ZmYtZjQ2Yi00ODVmLTkzZjgtZmVkZDBkODk0MDFl'
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_CAINFO, $_SERVER['DOCUMENT_ROOT']."/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
I want to send JSON data to http://localhost:3000/certificates using curl in PHP
Here is what I do:
$url = 'http://localhost';
$ch = curl_init($url);
$jsonData = array(
'certno' => 'AB1234',
'issueDate' => 'asdasd',
'type' => 'asdasdasd'
);
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_PORT, 3000);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
How to add /certificates ? I cannot simply put it in url http://localhost/certificates right?
Hi is curl the best method for using POST with cookie and also navigated to another page for scrapping? I'm using the coding below and I can't get it to work.
include('simple_html_dom.php');
$data = array(
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATE' => '%2FwEPDwUKLTcyODA2ODEwMGRk',
'Myname' => 'justdev12345',
'Mypassword' => '12345',
'idLogin' => 'Login',
'tmplang2' => '6',
'fm' => '',
'jc' => '',
'LoginRef' => ''
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.site.com/mainframe.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_COOKIEJAR, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$output = new simple_html_dom();
$output = file_get_html('http://profiles.site.com/profile_900.aspx?AccountID=ShopCartUpdate');
print $output;
Your code is fine up until the end where you throw away the curl response and load the url with simple-html-dom. If you want to use the curl response it should look like this:
$html = str_get_html($output);
$html->find('title');
Otherwise you should probably remove all the curl code.