PHP Post with Header and XML Content - php

I want to subscribe a YouTube Channel through the GData API via PHP.
How can i do this post in PHP?
I tried it like this but the page keeps loading forever:
<?php
session_start();
include 'gdata.php' // For $DEVKEY
function post_xml($url, $xml) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array(
"Content-Type: application/atom+xml",
"Content-Length: 1024",
"Authorization: Bearer $token",
"GData-Version: 2",
"X-GData-Key: key=$DEVKEY"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $xml );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $result;
}
$token = $_SESSION['token'];
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<category scheme="http://gdata.youtube.com/schemas/2007/subscriptiontypes.cat"
term="channel"/>
<yt:username>GoogleDevelopers</yt:username>
</entry>';
$url = 'https://gdata.youtube.com/feeds/api/users/default/subscriptions';
echo post_xml($url, $xml);
?>
In HttpRequester i already managed it to do a HTTP Post Request and it worked. I think the problem is the content of the Request. How do i properly give the text which is in "Content" (look at the screenshot) via PHP (cURL)?
Thanks :)

Just put the $xml as POSTFIELDS and it should work:
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
And add the correct content-length:
"Content-Length: ".strlen($xml),
When you're planning to do more, like PUT-requests with data and just lots of REST-requests, I would suggest to use some kind of package like Httpful. Curl has it's pitfalls...

You can look at the code below, hope it will help
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($ch);
curl_close($ch);
return json_decode($res);

Related

PHP 7 cURL request gives response code 302 nginx

$data = array(
"data1" => "1000",
);
$data_string = json_encode($data);
$ch = curl_init("http://www.example.com/api/info/getPlan");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($data_string))
);
$result = curl_exec($ch);
https://i.stack.imgur.com/B9fKn.png
I'm trying to make a POST request from the portal with the following code. And got above error in browser. I'm using PHP 7.4,Apache 2.4.6 version.
I see some answer say to use CURLOPT_FOLLOWLOCATION or cookies and I try it but I cannot figure out how to do. Please some help is really appreciated.
Yes, using CURLOPT_FOLLOWLOCATION is the correct answer
follow this example to see if it work for you
$cookie_file_path = "/toyourfile";
$data = array(
"data1" => "1000",
);
$data_string = json_encode($data);
$ch = curl_init("http://www.example.com/api/info/getPlan");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
//curl_setopt($ch, CURLOPT_VERBOSE, true); // if you run on commandline this will print out more information for you
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Content-Length: " . strlen($data_string))
);
$result = curl_exec($ch);

how to implement soap request in php?

I searched this website for a helpful response for integrating a SOAP Request in PHP, but I didn't find anything that solved my problem.
I'm new to SOAP and can't figure out why I am not getting any response:
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>'.
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'.
' xmlns:xsd="http://www.w3.org/2001/XMLSchema"'.
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'.
'<soap:Body>'.
'<food xmlns="http://www.localhost:81/dbWIP/xml1.xml">'.
'<price>$5.95</price>'.
'</food>'.
'</soap:Body>'.
'</soap:Envelope>';
$url = "http://www.localhost:81/dbWIP/xml1.xml";
$ch = curl_init();
echo $ch;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$headers = array();
array_push($headers, "Content-Type: text/xml; charset=utf-8");
array_push($headers, "Accept: text/xml");
array_push($headers, "Cache-Control: no-cache");
array_push($headers, "Pragma: no-cache");
array_push($headers, "SOAPAction:http://www.localhost:81/dbWIP/xml1.xml");
if($xml != null) {
echo $xml;
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
array_push($headers, "Content-Length: " . strlen($xml));
}
curl_setopt($ch, CURLOPT_USERPWD, "user_name:password"); /* If required */
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
echo $response;
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo"<br/> CODE:"$code;
curl_close($ch);
?>
I think you are missing URL from curl_init which will initialize a cURL session, but yours has no URL.
$curl = curl_init("http://www.localhost:81/dbWIP/xml1.xml");
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
And try setting your headers to
$headers = array(
'Content-type: text/xml;charset="utf-8"',
'Accept: text/xml',
'Cache-Control: no-cache',
'Pragma: no-cache',
'Content-length: ' . strlen($xml),
);
Try to remove the quotation marks:
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

How to send Arabic SMS using Clickatell REST API in PHP?

Here is my code
$to="[\"<mobile number>\"]";
$text = "غثس هفس خن";
$arr = unpack('H*hex', iconv('UTF-8', 'UCS-2BE', $text));
$message = strtoupper($arr['hex']);
$authToken="3fUXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.clickatell.com/rest/message");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\":\"$message\",\"to\":$to}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Version: 1",
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Bearer $authToken"
));
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
When I am sending using this, I am getting some numbers as SMS
From Clickatell document, I got one solution that add a parameter named 'unicode' as '1'.
Then I changed the parameter line like this
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\":\"$message\",\"to\":$to\",\"unicode\":1}");
Now I am getting {"error":{"code":"100","description":"Data malformed","documentation":"http://www.clickatell.com/help/apidocs/error/100.htm"}}

PHP XML request to SOAP WebService

Sorry if i am asking something that was already discussed here but all my combinations that i tryed are not working. I am not PHP developer but i am trying create PHP sample that will send request to my SOAP asp.net webservice and to echo the response.
I am trying to use the following PHP code:
$soapUrl = "https://test-api.xxxx.xx/reseller.asmx?op=loginXML";
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><loginXML xmlns="https://test-api.zemi.mk/"><acckey>adsdsad</acckey><password>pass</password></loginXML></soap12:Body></soap12:Envelope>';
$headers = array(
"POST /reseller.asmx HTTP/1.1",
"Host: test-api.*****.**",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
$parser = simplexml_load_string($response2);
echo $parser;
But it does not work.
What i am seeing then i am trying to execute is:
$soapUrl = "https://test-api.++++.++/reseller.asmx?op=loginXML"; $xml_post_string = 'adsdsadpass'; $headers = array( "POST /reseller.asmx HTTP/1.1", "Host: test-api.++++.++", "Content-Type: application/soap+xml; charset=utf-8", "Content-Length: ".strlen($xml_post_string) ); $url = $soapUrl; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); $response1 = str_replace("","",$response); $response2 = str_replace("","",$response1); $parser = simplexml_load_string($response2); echo $parser;
Can u help me what i am doing wrong.
Thanks

getting data from server in xml format

I am trying to call a web service through php . I am sending an XML request for which I am supposed to get back an Xml response. But I as response I am getting only the data without the xml tags. I want it in proper xml format. I also have the wsdl file of the website. If anyone can advise what to do it will be of real help. Thanks in advance for your time.
<?php
$soapUrl=some url';
$xml_post_string='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sec="http://schemas.xmlsoap.org/ws/2002/04/secext" xmlns:urn="urn:ebx-schemas:dataservices_1.0">
<soapenv:Header>
<sec:Security>
<UsernameToken>
<Username>Some Value</Username>
<Password>Some Value</Password>
</UsernameToken>
</sec:Security>
</soapenv:Header>
<soapenv:Body>
<urn:select_ProductDetail>
<branch>Product</branch>
<instance>Product</instance>
<viewPublication>List Name</viewPublication>
</urn:select_ProductDetail>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-Type: text/xml;charset=UTF-8",
"Accept: gzip,deflate",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: \"select\"",
"Host: Host name",
"Connection: Keep-Alive",
"Content-length: ".strlen($xml_post_string),
);
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($ch, CURLOPT_MAXREDIRS, 12);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ss = curl_getinfo($ch);
$response = curl_exec($ch);
print_r($response);
exit;
curl_close($ch);
?>
Try modifying your codes to below, for viewing it in BROWSERS
$ss = curl_getinfo($ch);
$response = curl_exec($ch);
echo '<pre>';
echo htmlspecialchars(print_r($response, true));
echo '</pre>';
#print_r($response);
exit;
curl_close($ch);
You are not getting output in xml format because output is getting concatenated with header of curl to avoid so use below code
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 500);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
curl_setopt($ch, CURLOPT_MAXREDIRS, 12);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$ss = curl_getinfo($ch);
//$response will give you xml format code
$response = curl_exec($ch);
//$xml will create array from that xml format code
$xml=simplexml_load_string($response);
print_r($xml);
exit;
curl_close($ch);

Categories