JTapp Posted February 2, 2014 Author Share Posted February 2, 2014 Again. I'm scouring the internet trying to teach myself. The only help I'm asking for is direction to a resource. Somebody out there might be able to direct me to a resource so I can learn how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467459 Share on other sites More sharing options...
JTapp Posted February 2, 2014 Author Share Posted February 2, 2014 For clarity's sake- I am trying to learn how to open a file on an IP address within my PHP. If I need to go take a course in cURL (because that's the advice you guys would give) Then I have my answer. Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467460 Share on other sites More sharing options...
boompa Posted February 2, 2014 Share Posted February 2, 2014 https://www.google.com/search?q=calling+a+rest+api+from+php Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467461 Share on other sites More sharing options...
ignace Posted February 2, 2014 Share Posted February 2, 2014 (edited) From what I gather from your posts, this contains your missing puzzle pieces: function http_json_post($url, $data) { $ch = curl_init($url); curl_setopt_array($ch, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $data_string = json_encode($data), // your data in JSON: {"name":"JTapp","age":"50"} CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', // tell the server we are sending it JSON 'Content-Length: ' . strlen($data_string)) // tell it how long it is ) )); $output = curl_exec($ch); curl_close($ch); return $output; } $webservice_url = 'http://webservice.local/data.json'; // actual API URL $result = http_json_post($webservice_url, array('name' => 'JTapp', 'age' => 50));If this does not work, then state the returned error. Edited February 2, 2014 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467463 Share on other sites More sharing options...
.josh Posted February 2, 2014 Share Posted February 2, 2014 There are several methods for requesting something from another site, from fopen to file_get_contents to curl to sockets to using ftp. But there's nothing we can really do to help your specific issues with them, unless we know specific details about the problem. What does the remote server expect in the request? What does it give as a response? etc.. No, you don't have to post code, and we do in fact warn against posting code that you do not want to have publicly available. We provide free help when we are able, and the exchange for that is that it's public. That's how most "free" sites work. If you want private help, you will have to pay for it (e.g. hire a freelancer). So you can't post the code because it's private. But you can't get help without posting code. You are right; it is sort of a catch-22. But how on earth do you expect us to help when you don't? Are you really getting upset at us because we aren't psychic? Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467464 Share on other sites More sharing options...
JTapp Posted February 2, 2014 Author Share Posted February 2, 2014 ignace - I found an extra closed parenthesis and got rid of it, then the last line caused a hang up and ultimately it produced an internal server error upon removing it I got zero errors, but I also got zero data I tried changing $result to the $echo line as shown below - which is the code I'm using - but now all I'm getting is the single word "Array" Thank you <?php $url = 'myapiurl'; $data = array('a_field_title' => 'desired_result', 'a_field_title2' => 'desired_result2'); function http_json_post($url, $data) { $curl = curl_init($url); curl_setopt_array($ch, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $data_string = json_encode($data), // your data in JSON CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', // tell the server we are sending it JSON 'Content-Length: ' . strlen($data_string)) // tell it how long it is ) ); $output = curl_exec($curl); curl_close($curl); return $output; } $webservice_url = 'actualAPI_URL'; // actual API URL echo $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467472 Share on other sites More sharing options...
Ch0cu3r Posted February 3, 2014 Share Posted February 3, 2014 Look at ignaces code again. You have not called http_json_post(). You should be passing $url as the first argument and then $data as the second argument. $result should then contain the json returned from your api. To convert the json into an array use json_decode Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467522 Share on other sites More sharing options...
JTapp Posted February 3, 2014 Author Share Posted February 3, 2014 The API developer - "The API will pass back XML formatted or JSON formatted and it is in compliance with Web API 2. GET api?/productname?requiredfieldID={desiredfieldID} The requiredfieldID has to be defined in the request URI" This is where I'm getting hung up on the json_decode and how the array is configured. Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467602 Share on other sites More sharing options...
JTapp Posted February 3, 2014 Author Share Posted February 3, 2014 Look at ignaces code again. You have not called http_json_post(). You should be passing $url as the first argument and then $data as the second argument. $result should then contain the json returned from your api. To convert the json into an array use json_decode Based on this feedback I sat through a json course on lynda.com looking for anything that spoke to http_json_post() Now I understand that is not a standard item. I've also read ALOT about json_decode and have explored the world of $params Figuring I needed to utilize the API documentation, I tried to insert $params = array('field name' => 'desired result'); I believe I've gone full circle. Don't be hatin' though Three days of non-stop attempted self-education has demonstrate that I'm trying, right? Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467632 Share on other sites More sharing options...
JTapp Posted February 4, 2014 Author Share Posted February 4, 2014 Ok.. in the off chance the recommendation was a typo, I changed http_json_post to http_post_data That didn't do anything. I can't seem to find consistency on the internet regarding how to write what is in the API Documentation: GET api/NAMEOFAPI?PrimaryNameID={DesiredPrimaryNameID} The error message that I'm getting is Internal Server Error I'm sure it has to do with how I'm posting the parameters. Its also clear that I don't understand the difference between $url and $webservice_url. All help is appreciated.. <?php $url='http://xx.xxx.org/xxx/xxx/xxxWebApi.mode'; //Am I supposed to put a concatenated ? after .mode ? $data = array(NameOfField => 'IDnumber'); function http_json_post($url, $data) { $ch = curl_init($url); curl_setopt_array($ch, array( CURLOPT_RETURNTRANSFER => 1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $data_string = json_encode($data), CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', // tell the server we are sending it JSON 'Content-Length: ' . strlen($data_string)) // tell it how long it is ) ); $output = curl_exec($ch); curl_close($ch); return $output; } $webservice_url = 'http://xx.xxx.org/xxx/xxx/xxxWebApi.mode/api/NAMEofAPI?NameOfField=Value'; // actual API URL $result = http_json_post($webservice_url, array('NameofField' => 'value')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467643 Share on other sites More sharing options...
kicken Posted February 4, 2014 Share Posted February 4, 2014 Don't be hatin' though Three days of non-stop attempted self-education has demonstrate that I'm trying, right? It's not that we are being haters, or that we don't think you are trying. We simply cannot provide any sort of help with the information you are willing to provide. It's like if you ride your bicycle up to the auto-mechanics to tell them your car is broken and ask for help fixing it, but when they ask what's the issue is or if they can see your car you say "Sorry, that's private". Ok.. in the off chance the recommendation was a typo, I changed http_json_post to http_post_data The code given to use was a custom function. That is why there is no mention of it in the tutorials/classes you may have watched. Changing the name will not do anything to affect the outcome either. I can't seem to find consistency on the internet regarding how to write what is in the API Documentation: GET api/NAMEOFAPI?PrimaryNameID={DesiredPrimaryNameID} There really isn't any consistency for how an API accepts or returns data. That is why we cannot help you because in order to we'd need to be able to see the API (or documentation at least) to determine how it works. Every API is different with regard to how you pass data to it or get data from it. JSON or XML are common mediums for transferring data but that tells you nothing about the actual data you need to send/get (ie, parameter names, order of fields, etc). Based on what you have stated the API seems to expect a GET request with various parameters in the query string of the URL. What this means is you can use a simple file_get_contents to query the API and see the results without having to mess around with trying to send POST data which is what the help you have thus far received has been oriented toward. Being a simple GET request means you can test the API your browser also by simply entering the URL into your address bar and checking the data returned. Once you get data you expect you can start on your PHP code to issue the request and work with the result. So, here is the basic steps of how you start trying do what you need: Start with your browserInput the Web API URL in your address bar Add the parameters you think it needs in the query string and load the page If you get an error, repeat step b. If you get expected results, move on Code your PHPTake the URL from your address bar and put it into your PHP code as a variable; $url="copy/paste url here" For a simple GET request, all you need to do is use file_get_contents to load response. Use the URL as the first parameter, assign the result to a variable var_dump the variable returned by file_get_contents and ensure it contains similar content to what shows in the browser when you make the api request. When var_dump shows the expected results, move on Process the resultsDetermine what kind of result you are getting. Is it XML? JSON? Something else. JSON is common because it is easy, but XML is still used. If you are getting a JSON result you would use json_decode to convert the data into a PHP object or array for processing. If you are getting a XML result then you'll need to use either SimpleXML or DOMDocument to access the information. In order for us to provide any more specific help than that you need to be able to provide us with more specific information such as API documentation. You can always try and edit out whatever confidential/proprietary information you need to but you still need to convey the usage of the API. Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467645 Share on other sites More sharing options...
JTapp Posted February 4, 2014 Author Share Posted February 4, 2014 (edited) The following code worked. After researching and discovering there are curl issues with the php.ini file - and choosing not to edit the .ini, I found this absolute alternative It ain't pretty but I didn't expect it to be. <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.apiwebsite.com"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); curl_close($ch); $result = preg_replace("#(<\s*a\s+[^>]*href\s*=\s*[\"'])(?!http)([^\"'>]+)([\"'>]+)#",'$1http://www.apiwebsite.com/$2$3', $result); echo $result ?> Edited February 4, 2014 by JTapp Quote Link to comment https://forums.phpfreaks.com/topic/285873-crap-ive-been-asked-to-change-my-php-its-been-working-for-years/page/2/#findComment-1467661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.