I'm trying to send XML to a remote site. I originally coded it using curl. That failed (no response) and the rep for the company said it was because I was using curl. So I changed the code to not use it but it fails with
file_get_contents(https://testws.atdconnect.com/ws/3_4/locations.wsdl/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
The code I am using is shown below. The first call tries to send with curl while the second doesn't. Can someone point out where I am going wrong, please?
$client_id = "the id";
$username = "the user";
$password = "the pass";
$url = "https://testws.atdconnect.com/ws/3_4/locations.wsdl/";
echo GetDistributionCenter($url, $client_id, $username, $password);
echo GetDistributionCenter($url, $client_id, $username, $password, true);
function GetDistributionCenter($url, $client_id, $username, $password, $send_array = false) {
$xml_str = '
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="http://api.atdconnect.com/atd/3_4/locations">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken atd:clientId="' . $client_id . '" xmlns:atd="http://api.atdconnect.com/atd">
<wsse:Username>' . $username . '</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' . $password . '</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<loc:getDistributionCenterRequest>
<loc:servicingDC>070</loc:servicingDC>
</loc:getDistributionCenterRequest>
</soapenv:Body>
</soapenv:Envelope>';
if ($send_array) {
$xml_str = array($xml_str);
$xml_str = http_build_query($xml_str);
return SendPostRequest($url, $xml_str);
}
return SendXMLCommand($url, $xml_str);
}
function SendPostRequest($url, $postdata) {
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($postdata) . "\r\n",
'content' => $postdata
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
function SendXMLCommand($url, $parsed_data) {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt ($ch, CURLOPT_POSTFIELDS, $parsed_data);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));
$result = curl_exec($ch);
if (curl_error($ch)) {
$result = 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $result;
}
Related
I'm having a problem with PHP's cURL returning an empty string with some URL's
i want to coonect this api and i give the empty result, anyone can help me ?
<?php
$clTrid="TEST-23233434343";
$nictoken="6661361102210630";
$contact="Test-irnic";
$xml = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command>
<check>
<contact:info xmlns:contact="http://epp.nic.ir/ns/contact-1.0">
<contact:id>' . $contact . '</contact:id>
<contact:pw>' . $nictoken . '</contact:pw>
</contact:authInfo>
</contact:check>
</check>
<clTRID>' . $clTrid . '</clTRID>
</command>
</epp>';
$url = 'https://epp.nic.ir/submit';
$curl = curl_init($url);
curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
}
else echo " No Error ! ";
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
}
curl_close($curl);
echo $result;
var_dump($result);
?>
I was trying to convert curl to use guzzle but I got an error cURL error 56: Failure when receiving data from the peer.
Below is what I have so far.
curl_test.php which is working
$username = "admin";
$password= "password";
$request_xml = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
<person>
<individual>
<user_id>1234</user_id>
<full_name>test</full_name>
</individual>
</body>
</person>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, "XML_STRING=" . $request_xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
$response = curl_exec($ch);
return $response;
guzzle_test.php not working
$request_xml = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
<person>
<individual>
<user_id>1234</user_id>
<full_name>test</full_name>
</individual>
</body>
</person>';
$client = new Client(["verify"=>false,
"auth"=>["admin","password"
]
]);
$options = [
"headers"=>
["Content-Type"=>"text/xml"],
"body"=>$requestXml
];
$response = $client->request("POST",$url,$options]);
$res = $response->getBody();
Can you help me with this?
Any help is greatly appreciated.
Thanks
You need to use form_params instead of body to send a form to the server. Try the code below.
$request_xml = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE telco_xml_enq SYSTEM "telco_xml_enq.dtd">
<person>
<individual>
<user_id>1234</user_id>
<full_name>test</full_name>
</individual>
</body>
</person>';
$client = new Client(["verify"=>false,
"auth"=>["admin","password"
]
]);
$options = [
"form_params"=>[
'XML_STRING' => $requestXml,
],
];
$response = $client->request("POST",$url,$options]);
$res = $response->getBody();
Looks like you have to use XML_STRING for your post values in the body. Due to this receiver is not able to retrieve the values and hence throws an error.
$options = [
"headers"=>
["Content-Type"=>"text/xml"],
"body" => "XML_STRING=".$requestXml
];
I've never had the opportunity to submit to a WSDL SOAP web service and am running into some issues. Im using PHP cURL to submit the form to a known back end fist, then secondly to the WSDL SOAP service. The first part is working fine, so I will skip over that. I have spent the better part of 3 days trying different solutions I've found on the web, and my own after reading SOAP documentation, with no luck.
Here is what I'm using to submit to the WSDL
<?php
//first cURL POST HERE - works fine
//second cURL POST BELOW
$FName = $_POST['FirstName'];
$Lname = $_POST['LastName'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone1'];
$soapURL = "https://something.com/IBWeb/IBDemoManager/IBDemoManager.asmx?wsdl";
$soapUser = "USR";
$soapPassword = "PWD";
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$xml_post_string = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservices.htdocs.openecry">
<soapenv:Header/>
<soapenv:Body>
<web:demosetup soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<AccessCode xsi:type="xsd:string">G0!=#%fut40</AccessCode>
<NewUserCategoryName xsi:type="xsd:string">OFLDemo</NewUserCategoryName>
<TemplateUserName xsi:type="xsd:string">OFLUser</TemplateUserName>
<CusType xsi:type="xsd:string">Indirect</CusType>
<WLabelID xsi:type="xsd:string">276</WLabelID>
<SCodeID xsi:type="xsd:string"></SCodeID>
<SoftID xsi:type="xsd:string">1</SoftID>
<FName xsi:type="xsd:string">'.$FName.'</FName>
<LName xsi:type="xsd:string">'.$LName.'</LName>
<Email xsi:type="xsd:string">'.$Email.'</Email>
<Phone xsi:type="xsd:string">'.$Phone.'</Phone>
<Address xsi:type="xsd:string"></Address>
<City xsi:type="xsd:string"></City>
<Zip xsi:type="xsd:string"></Zip>
<State xsi:type="xsd:string"></State>
<Country xsi:type="xsd:string"></Country>
<CountryName xsi:type="xsd:string"></CountryName>
<AssetTypes xsi:type="xsd:string">Futures</AssetTypes>
<How xsi:type="xsd:string">OFL webservice</How>
<MoreEmail xsi:type="xsd:string"></MoreEmail>
<RemoteAddr xsi:type="xsd:string">'.$hostname.'</RemoteAddr>
<CampaignID xsi:type="xsd:string"></CampaignID>
</web:demosetup>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
//IS SOAPAction the same as the endpoint "$soapURL"?//
"SOAPAction: https://something.com/IBWeb/IBDemoManager/IBDemoManager.asmx?wsdl",
"Content-length: ".strlen($xml_post_string),
);
$url2 = $soapURL;
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url2 );
curl_setopt($soap_do, CURLOPT_HEADER, false);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
if(curl_exec($soap_do) === false) {
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
} else {
$result = curl_exec($soap_do);
echo '<pre>';
print_r($result);
curl_close($soap_do);
//print 'Operation completed without any errors';
}
Here are just some comments:
Try to disable the SSL check (just for testing):
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
You should call curl_close($ch2); as last. Example:
$output2 = curl_exec($ch2);
if(curl_errno($ch2))
echo curl_error($ch2);
} else {
echo $output2;
}
curl_close($ch2); // <--- close here
You could also try the Zend SOAP library.
If you don't like CURL try Guzzle to make an HTTP request.
I tried to add contact to google through PHP
I add a new project to google developer, and activate contact API for this email
and I used the below code, but the response as the following: json_response: { "error" : "invalid_request", "error_description" : "Could not determine client ID from request." }
could you help me to fix this issue
parameters:
code: id from database for example: 101
gsm: cell phone
Code:
<?php
$code=doubleval($_REQUEST['code']);
echo 'code: '.$code.'<br />'.PHP_EOL;
$access = get_oauth2_token($code);
echo 'access: '.$access.'<br />'.PHP_EOL;
$client_id= 'hazem*********#gmail.com';
$client_secret= '***********';
$redirect_uri= 'http://************/synchronize.php';
$gsm=doubleval($_REQUEST['gsm']);
echo 'gsm: '.$gsm.'<br />'.PHP_EOL;
if($gsm){
addContact($access, $gsm);
}
//returns session token for calls to API using oauth 2.0
function get_oauth2_token($code) {
global $client_id;
global $client_secret;
global $redirect_uri;
$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);
$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
echo 'json_response: '.$json_response.'<br />'.PHP_EOL;
curl_close($curl);
$authObj = json_decode($json_response);
if (isset($authObj->refresh_token)){
//refresh token only granted on first authorization for offline access
//save to db for future use (db saving not included in example)
global $refreshToken;
$refreshToken = $authObj->refresh_token;
}
$accessToken = $authObj->access_token;
return $accessToken;
}
function addContact($access, $gsm){
$name =$gsm;
$fullName=$gsm;
$last= $gsm;
/*
<gd:givenName>'.$name.'</gd:givenName>
<gd:familyName>'.$last.'</gd:familyName>
//*/
$contactXML = '<?xml version="1.0" encoding="utf-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:fullName>'.$fullName.'</gd:fullName>
</gd:name>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">'.$gsm.'</gd:phoneNumber>
</atom:entry>';
echo $contactXML;
$headers = array(
'Host: www.google.com',
'Gdata-version: 3.0',
'Content-length: '.strlen($contactXML),
'Content-type: application/atom+xml',
'Authorization: OAuth '.$access
);
$contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_exec($ch);
}
?>
Client ID is the ID of your client application that you registered in the dev console (issued with your client secret). Not the email of the user.
I am Working send message using youtuba api. But i got a Error on my file. it shows Invalid Token 401 Error. My file is given below.I'm pretty sure I must be missing something vital but small enough to not notice it.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = array('accountType' => 'GOOGLE',
'Email' => 'User Email',
'Passwd' => 'pass',
'source'=>'PHI-cUrl-Example',
'service'=>'lh2');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$kk = curl_getinfo($ch);
$response = curl_exec($ch);
list($auth, $youtubeuser) = explode("\n", $response);
list($authlabel, $authvalue) = array_map("trim", explode("=", $auth));
list($youtubeuserlabel, $youtubeuservalue) = array_map("trim", explode("=", $youtubeuser));
$developer_key = 'AI39si7SavL5-RUoR0kvGjd0h4mx9kH3ii6f39hcAFs3O1Gf15E_3YbGh-vTnL6mLFKmSmNJXOWcNxauP-0Zw41obCDrcGoZVw';
$token = '7zWKm-LZWm4'; //The user's authentication token
$url = "http://gdata.youtube.com/feeds/api/users/worshipuk/inbox" ; //The URL I need to send the POST request to
$title = $_REQUEST['title']; //The title of the caption track
$lang = $_REQUEST['lang']; //The languageof the caption track
$transcript = $_REQUEST['transcript']; //The caption file data
$headers = array(
'Host: gdata.youtube.com',
'Content-Type: application/atom+xml',
'Content-Language: ' . $lang,
'Slug: ' . rawurlencode($title),
'Authorization: GoogleLogin auth='.$authvalue,
'GData-Version: 2',
'X-GData-Key: key=' . $developer_key
);
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<id>Qm6znjThL2Y</id>
<summary>sending a message from the api</summary>
</entry>';
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_HEADER, TRUE );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($xml) );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1 );
$tt = curl_getinfo($ch);
print_r($tt);
$result = curl_exec($ch);
print_r($result);
exit;
// close cURL resource, and free up system resources
curl_close($ch);
?>
any problem in my code? Please guide me. How can I get a result from this code?
Most likely your authentication is wrong, please debug that part first. Either you are not using right scope or that API is not enabled from your console.
On a separate note, I strongly suggest to use Youtube Data API v3 for this. We have updated PHP client library and great samples to get you started.