I want to add a contact with data to google contacts.i am getting error as "There was an error in your request. That's all we know."
The code is as follows
<?php
$contact_detail='<?xml version="1.0"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
<gd:name>
<gd:firstName>John</gd:firstName>
<gd:additionalName> test</gd:additionalName>
<gd:givenName>Doe</gd:givenName>
</gd:name>
<gd:email address="john#doe.com" rel="http://schemas.google.com/g/2005#work"/>
<gd:email address="john2#doe.com" rel="http://schemas.google.com/g/2005#home"/>
<gd:organization rel="http://schemas.google.com/g/2005#work">
<gd:orgName>John Deere</gd:orgName>
<gd:orgTitle>Owner</gd:orgTitle>
</gd:organization>
</atom:entry>';
$url="https://www.google.com/m8/feeds/contacts/default/full";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$contact_detail");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
print($output);
curl_close($ch);
https://developers.google.com/google-apps/contacts/v2/developers_guide_protocol
To publish this entry, send it to the contact-list post URL as follows. First, place your Atom element in the body of a new POST request, using the application/atom+xml content type. Then send it to the post URL. For example, to add a contact to the contact list belonging to liz#gmail.com, post the new entry to the following URL: https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full
Related
I am currently trying to send information to eBay to set the orders shipment/tracking details. At the moment I am getting a 'success' message but no information is update on eBay's website.
I have been using the links below to guide me on implementing this change:
http://developer.ebay.com/devzone/large-merchant-services/Concepts/MakingACall.html
http://developer.ebay.com/devzone/merchant-data/CallRef/SetShipmentTrackingInfo.html#Samples
The format of the XML I am sending back is in the exact format as described, please see below the details I am sending and the PHP used to send via CURL through eBay's API.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<BulkDataExchangeRequests>
<Header>
<Version>591</Version>
<SiteID>0</SiteID>
</Header>
<SetShipmentTrackingInfoRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<OrderID>261671515555-0</OrderID>
<OrderLineItemID>261672341232</OrderLineItemID>
<Shipment>
<ShipmentTrackingNumber>JD0002250296232332</ShipmentTrackingNumber>
<ShippedTime>2014-11-27T14:41:27\Z</ShippedTime>
<ShippingCarrierUsed>Yodel</ShippingCarrierUsed>
</Shipment>
</SetShipmentTrackingInfoRequest>
</BulkDataExchangeRequests>
PHP:
$xml_request = "";
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $ebay_url);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $xml_request);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($connection);
curl_close($connection);
var_dump($response);
The issue was the provider of the API was having intermittant issuses.
I'm using Google's own example from https://developers.google.com/google-apps/spreadsheets/
They say:
To update the contents of an existing row, first retrieve the row to update, modify it as desired, and then send a PUT request, with the updated row in the message body, to the row's edit URL.
Be sure that the id value in the entry you PUT exactly matches the id of the existing entry. The edit URL is highlighted in the following row entry:
<entry gd:etag='"S0wCTlpIIip7ImA0X0QI"'>
<id>https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId</id>
<updated>2006-11-17T18:23:45.173Z</updated>
<category scheme="http://schemas.google.com/spreadsheets/2006"
term="http://schemas.google.com/spreadsheets/2006#list"/>
<title type="text">Bingley</title>
<content type="text">Hours: 10, Items: 2, IPM: 0.0033</content>
<link rel="self" type="application/atom+xml"
href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId"/>
<link rel="edit" type="application/atom+xml"
href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId/version"/>
<gsx:name>Bingley</gsx:name>
<gsx:hours>20</gsx:hours>
<gsx:items>4</gsx:items>
<gsx:ipm>0.0033</gsx:ipm>
</entry>
I've been attempting to send this info (using my own data, but exact same structure) with the following CURL:
$headers = array(
"Authorization: GoogleLogin auth=" . $google_auth,
"GData-Version: 3.0",
"Content-Type: application/atom+xml",
"If-Match: *",
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/feeds/list/$feed/0/private/full/$row_id");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = curl_exec($curl);
echo $response;
It seems that the XML is malformed which is odd as it comes right from Google's example and I get the same response when retrieving a list feed from my own spreadsheets. How do I correct and are there any other issues?
When I paste this xml into http://www.freeformatter.com/xml-formatter.html, I receive an error that
Unable to parse any XML input. org.jdom2.input.JDOMParseException: Error on line 1: The prefix "gd" for attribute "gd:etag" associated with an element type "entry" is not bound.
Thanks.
Well, no real responses....but I finally got it working. Hopefully this helps someone else. The idea is you need to use the namespaces which are retrieved with the feed.
So the XML should look like:
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" gd:etag=""S0wCTlpIIip7ImA0X0QI"">
<id>https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId</id>
<updated>2006-11-17T18:23:45.173Z</updated>
<category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#list" />
<title type="text">Bingley</title>
<content type="text">Hours: 10, Items: 2, IPM: 0.0033</content>
<link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId" />
<link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full/rowId/version" />
<gsx:name>Bingley</gsx:name>
<gsx:hours>20</gsx:hours>
<gsx:items>4</gsx:items>
<gsx:ipm>0.0033</gsx:ipm>
</entry>
And the CURL should look like:
$headers = array(
"Authorization: GoogleLogin auth=" . $google_auth, //google_auth retrieved earlier
"GData-Version: 3.0",
"Content-Type: application/atom+xml",
"If-Match: *",
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/feeds/list/$feed/0/private/full/$id"); //feed is obtained from spreadsheet url and id can be obtained by retrieving list feed
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$response = curl_exec($curl);
echo $response;
If you want to retrieve and update a proper custom XML instead of a generic Atom XML doc, then you can try APISpark PaaS which has a wrapper for GSpreadsheets that can create and host a custom web API.
See tutorial: https://apispark.com/docs/tutorials/google-spreadsheet
This should be help to you
$accessToken = $ACCESS_TOKEN;
$key = $SPREADSHEET_ID;
$sheetId = $SHEET_ID;
$editUrl = "https://spreadsheets.google.com/feeds/cells/$key/$sheetId/private/full/R2C4?access_token=$accessToken";
$entry = '<?xml version="1.0" encoding="UTF-8" ?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006"><id>https://spreadsheets.google.com/feeds/cells/'.$key.'/'.$sheetId.'/private/full/R2C4</id><link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/'.$key.'/'.$sheetId.'/private/full/R2C4"/><gs:cell row="2" col="4" inputValue="Enter_Value_Here">Enter_Value_Here</gs:cell></entry>';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $editUrl );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml','If-Match: *','Content-length: ' . strlen($entry)));
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $entry );
$content = curl_exec($ch);
if($content === false)
{
echo 'Curl error: ' . curl_error($ch)."<br>";
}
else
{
echo 'Operation completed without any errors <br>';
$information = curl_getinfo($ch);
}
curl_close($ch);
I am getting a Premature end of file response to my XML request for following code.
Cant figure out where is the error.
$xml = '<?xml version="1.0" ?>';
$xml .= '<PickUpCityListRQ>';
$xml .= '<Credentials username="'.$api->username.'" password="'.$api->password.'" remoteIp="'.$api->remoteIp.'" />';
$xml .= '<Country>UK</Country>';
$xml .= '</PickUpCityListRQ>';
$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?serverName=www.rentalcars.com&xml='.utf8_decode(trim($xml));
$port = 443;
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Fail on errors
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return into a variable
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 15s
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, '&xml='.$xml); // add POST fields
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
if ($port==443) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml; charset=UTF-8', 'Accept: application/xml; charset=UTF-8'));
$data = curl_exec($ch);
curl_close($ch);
Have tried sending data in post fields as well but did not work.
Response:
<!DOCTYPE DefaultRS SYSTEM 'https://xml.rentalcars.com:443//tj.dtd'><DefaultRS>
<Error id="2">
<Message>Premature end of file.</Message>
</Error>
</DefaultRS>
Any help appreciated.
Thanks
You need to url encode the xml data before you can use it as a query parameter. So the url assignment becomes:
$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?
serverName=www.rentalcars.com&xml='.urlencode(utf8_decode(trim($xml)));
Note, I've just wrapped that for readability - that should obviously be on one line.
After trying a lot of variation I finally managed to make it work.
There were two things that were casuing problem.
utf8_decode should be utf8_encode
POSTFIELDS data is supposed to be an array
otherwise parser will throw Premature end of file or not send back anything.
I am trying to get response form my API via CURL, the method needs to get user data via XML and than returns XML with response data.
I get nothing back. API works well, when I try the same request elsewhere than in PHP, it works. My API requires the data to be sent in application/xml Content-Type.
Any ideas what I am doing wrong?
<?php
$request="https://api.mydomain.com/operation/v1/rest/xml/user/authenticate";
$xml_data='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<userCredentials>
<userName>name</userName>
<password>password</password>
</userCredentials>';
$ch = curl_init();
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_URL, $request);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-type: application/xml");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo "<b>Request:</b> $request <br/>";
echo "<b>Response:</b><pre>".$output."</pre>";
?>
As commented, quick guess (lengthy comment):
echo "<b>Response:</b><pre>".$output."</pre>";
This will output the XML unescaped. Your browser will not display anything inside the browser window, but you should be fine to see something when you view the source of the page.
Because the received string has a lot of gibberish in it and I can't use it as an xml. Any thoughts?
$login_xml ='<xml>'.
'<action>log_in</action>'.
'<parameters>'.
'<username>'.$var.'</username>'.
'<pass>abdef01</pass>'.
'</parameters>'.
'</xml>';
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $login_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
$output = curl_exec($ch);
Maybe some options in the CURLOPT_HTTPHEEADER? Thanks for your time!
edit:
var_dump($output) : string '<xml><action>log_in</action><parameters><username>Ionel P</username><pass>abdef01</pass></parameters></xml>107' (length=110)
edit 2:
<?php
$xml = readfile('php://input');//file_get_contents('php://input');
var_dump($xml);
print_R($xml);
?>
var_dump($xml) = pre class='xdebug-var-dump' dir='ltr'><small>int</small> <font color='#4e9a06'>107</font>
107' (length=207)